根据操作者在键盘上所输入的城市及具体时间,自动爬取其相关的天气信息
未来天气信息:
这里预先在mysql数据库中存入相关的城市代码以方便等下目标网站的信息拼接
import requests
import json
import xlwt
import pymysql
#连接数据库
conn=pymysql.connect(host ="localhost" # 连接名称,默认127.0.0.1
,user = 'root' # 用户名
,passwd='13459562539zxj' # 密码
,port= 3306 # 端口,默认为3306
,db='data' # 数据库名称
,charset='utf8' # 字符编码
)
cur = conn.cursor() # 生成游标对象
name=input("请输入你要查询的城市名称:")
month=input("请输入你要查询的城市天气预报的时间:")
sql="select id号 from city where 城市='"+name+"'" # SQL语句
cur.execute(sql) # 执行SQL语句
data = cur.fetchall() # 通过fetchall方法获得数据
# print(data[0][0])
url = "http://d1.weather.com.cn/calendar_new/2021/"+data[0][0]+"_"+month+".html"
headers = {
"Referer":"http://www.weather.com.cn/",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}
r = requests.get(url=url, headers=headers)
if r.status_code == 200:
content = r.content.decode(encoding='utf-8')
weathers = json.loads(content[11:])
writebook = xlwt.Workbook()
sheet = writebook.add_sheet('Sheet1')
keys = ['date','nlyf','nl','w1','wd1','max','min','jq','t1','hmax','hmin','hgl','alins','als']
for i in range(len(keys)):
sheet.write(0, i, keys[i])
for i in range(len(weathers)):
for j in range(len(keys)):
sheet.write(i + 1, j, weathers[i][keys[j]])
writebook.save('weathers.xls')
历史天气:
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 目标url
city=input('请输入你所要获取信息的城市名称:')
month=input('请输入你所要获取信息的月份:')
url = 'http://www.tianqihoubao.com/lishi/'+city+'/month/'+month+'.html'
# 获取网页源代码
req = requests.get(url)
html = req.text
soup = BeautifulSoup(html,'html.parser')
tr_list = soup.find_all('tr')
datas,condition,temp = [], [], []
for data in tr_list[1:]:
sub_data = data.text.split()
a = sub_data[0].replace('年','/')
b = a.replace('月','/')
c = b.replace('日', '')
datas.append(c)
condition.append(''.join(sub_data[1:3]))
temp.append(''.join(sub_data[3:6]))
# 数据保存
data = pd.DataFrame() # 创建一个表格
data['日期'] = datas #向表格内添加数据
data['天气状况'] = condition
data['温度'] = temp
data.to_excel('C:/Users/JayDen/Desktop/指定城市历史天气数据/'+city+month+'天气记录.xls',index=False, encoding='utf-8')