爬取某地天气存入数据库
import requests
import pymysql
import json
from lxml import etree
db = pymysql.connect(
host='',
user='root',
password='',
db='weather',
port=,
charset='utf8',
)
url = "http://weather.cma.cn/api/now/需要某个城市的id"
urls = "http://weather.cma.cn/web/weather/需要某个城市的id.html"
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0',
"Referer": 'http://weather.cma.cn/web/weather/58321.html'
}
resp = requests.get(url=url, headers=headers)
resps = requests.get(url=urls, headers=headers)
resps.encoding = resps.apparent_encoding
tree = etree.HTML(resps.text)
weather = str(tree.xpath("//div[@id='dayList']/div/div[3]/text()")).replace('\\n', '').replace(' ', '').replace('\'','')[1]
datas = json.loads(resp.text)['data']['now']
precipitation = datas['precipitation']
temperature = datas['temperature']
pressure = datas['pressure']
humidity = datas['humidity']
windDirection = datas['windDirection']
windDirectionDegree = datas['windDirectionDegree']
windSpeed = datas['windSpeed']
windScale = datas['windScale']
lastUpdate = str(json.loads(resp.text)['data']['lastUpdate']).replace('/', '-')
sql = '''insert into yichang_weather(`precipitation`, `temperature`, `pressure`, `humidity`, `windDirection`, `windDirectionDegree`, `windSpeed`, `windScale`, `lastUpdate`,`weather`) values(\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\")'''.format(
precipitation, temperature, pressure, humidity, windDirection, windDirectionDegree, windSpeed, windScale,
lastUpdate, weather)
print(sql)
cursor = db.cursor()
cursor.execute(sql)
db.commit()
cursor.close()
db.close()