1.注册免费API和阅读技术文档:
注册地址:https://console.heweather.com
文档地址:https://www.heweather.com/documents/api-url
在文档中可以看到很多参数
返回的是json数据
2.获取API数据:
获取城市列表:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:XXC
import requests
url='https://cdn.heweather.com/china-city-list.txt'
strhtml = requests.get(url)
data = strhtml.text
data1 = data.split("\n")
for i in range(6): #因为前六行不需要,所以没有打印
data1.remove(data1[0])
for item in data1:
print(item[2:13])
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:XXC
import requests
import time
url='https://cdn.heweather.com/china-city-list.txt'
strhtml = requests.get(url)
data = strhtml.text
data1 = data.split("\n")
for i in range(6): #因为前六行不需要,所以没有打印
data1.remove(data1[0])
for item in data1:
# print(item[2:13])
url = 'https://free-api.heweather.com/v5/forecast?city='+item[2:13]+'&key=7d0daf2a85f64736a42261161cd3060b'
strhtml = requests.get(url)
time.sleep(1)
print(strhtml.text)
获取当天温度的最大值:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:XXC
import requests
import time
url='https://cdn.heweather.com/china-city-list.txt'
strhtml = requests.get(url)
data = strhtml.text
data1 = data.split("\n")
for i in range(6): #因为前六行不需要,所以没有打印
data1.remove(data1[0])
for item in data1:
# print(item[2:13])
url = 'https://free-api.heweather.com/v5/forecast?city='+item[2:13]+'&key=7d0daf2a85f64736a42261161cd3060b'
strhtml = requests.get(url)
time.sleep(1)
dic = strhtml.json()
for item in dic["HeWeather5"][0]["daily_forecast"]:
print(item["tmp"]["max"])
往mysql数据库中存入id,city,cnty三个字段数据:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:XXC
import json
import pymysql
import requests
import time def prem(db):
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print("Database version : %s " % data) # 结果表明已经连接成功
cursor.execute("DROP TABLE IF EXISTS weatherInfo") # 如果存在表就删除原表
sql = """CREATE TABLE weatherInfo (
id varchar(20),
city varchar(20),
cnty varchar(20))"""
cursor.execute(sql) # 根据需要创建一个表格 def reviewdata_insert(db):
url = 'https://cdn.heweather.com/china-city-list.txt'
strhtml = requests.get(url)
data = strhtml.text
data1 = data.split("\n")
for i in range(6): # 因为前六行不需要,所以没有打印
data1.remove(data1[0])
for item in data1:
# print(item[2:13])
url = 'https://free-api.heweather.com/v5/forecast?city=' + item[2:13] + '&key=7d0daf2a85f64736a42261161cd3060b'
strhtml = requests.get(url)
time.sleep(1)
dic = strhtml.json()
id = dic["HeWeather5"][0]["basic"]["id"]
city = dic["HeWeather5"][0]["basic"]["city"]
cnty = dic["HeWeather5"][0]["basic"]["cnty"]
result=[]
result.append((id,city,cnty))
insert_we = "insert into weatherInfo(id,city,cnty) values (%s,%s,%s)"
cursor = db.cursor()
cursor.executemany(insert_we, result)
db.commit() if __name__ == "__main__": # 起到一个初始化或者调用函数的作用
db = pymysql.connect("localhost", "root", "", "test", charset='utf8')
cursor = db.cursor()
prem(db)
reviewdata_insert(db)
cursor.close()
执行结果: