import requests
from bs4 import BeautifulSoup
from pyecharts import Bar
ALL_IN_ALL=[]
def get_url(url):
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\
(KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44",
"Referer": "https://cn.bing.com/"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content.decode('utf-8'), "html5lib")
conMidtabs=soup.find('div',class_='conMidtab')
tables=conMidtabs.find_all('table')
for table in tables:
tems=table.find_all('tr')[2:]
for index,tem in enumerate(tems):
tds=tem.find_all('td')
aderss=list(tds[0].stripped_strings)
if index==0:
aderss = list(tds[1].stripped_strings)
wind=tds[6].string
if index==0:
wind=tds[7].string
aderss.append(wind)
ALL_IN_ALL.append({"地点:":aderss[0],"最低气温:":int(aderss[1])})
def main():
url=["http://www.weather.com.cn/textFC/hb.shtml","http://www.weather.com.cn/textFC/db.shtml"\
,"http://www.weather.com.cn/textFC/hd.shtml","http://www.weather.com.cn/textFC/hz.shtml"\
,"http://www.weather.com.cn/textFC/hn.shtml","http://www.weather.com.cn/textFC/xb.shtml",\
"http://www.weather.com.cn/textFC/xn.shtml","http://www.weather.com.cn/textFC/gat.shtml"]
i=0
jindu=-5
print("====项目开始====")
while(i<8):
url_s=url[i]
#print(url_s)
get_url(url_s)
i+=1
print("进度为",jindu,"%")
jindu+=15
print("====任务完成====")
ALL_IN_ALL.sort(key=lambda data:data["最低气温:"])
for all_ in ALL_IN_ALL:
print(all_,"\n")
char=Bar("中国各地最低气温柱状图")
if __name__ == '__main__':
main()
一个简单的爬虫,适合初学者,不过很多地方要自己解决,csdn上都可以查得到。
教程网上有,但是要自己完善。