标签:temp 秦皇岛 bs4 天气情况 month 爬取 year import
一.本次用的是bs4方法解析
二.
上源代码
import requests
import re
import time
import xlwt
from bs4 import BeautifulSoup
import pandas as pd
from fake_useragent import UserAgent
date_box = []
max_temp = []
min_temp = []
weh = []
wind = []
for year in range(2011,2022):
for month in range(1,13):
print(f'爬取{year}年{month}月的天气数据')
if month<10:
month_str='0'+str(month)
else:
month_str=str(month)
url='https://lishi.tianqi.com/qinhuangdao/'+str(year)+month_str+'.html'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}
html=requests.get(url,headers=headers).text
bs=BeautifulSoup(html,'html.parser')
data=bs.find_all(class_='thrui')
date=re.compile('class="th200">(.*?)</')
tem=re.compile('class="th140">(.*?)</')
time1=re.findall(date,str(data))
print(time1)
time.sleep(0.05)
for item in time1:
date_box.append(item[:10])
temp=re.findall(tem, str(data))
print(temp)
temp_len=len(temp)
temp_len2=int(temp_len/4)
for i in range(temp_len2):
max_temp.append(temp[i * 4 + 0])
min_temp.append(temp[i * 4 + 1])
weh.append(temp[i * 4 + 2])
wind.append(temp[i * 4 + 3])
datas = pd.DataFrame({'日期':date_box,'最高温度': max_temp, '最低温度': min_temp, '天气': weh, '风向': wind})
print(datas)
datas.to_csv('./秦皇岛天气情况.csv',index=False,sep=',',encoding='utf-8_sig')
标签:temp,秦皇岛,bs4,天气情况,month,爬取,year,import 来源: https://blog.csdn.net/qq_52561726/article/details/121040790
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。