Python爬虫天气预报
2021/5/23 1:25:36
本文主要是介绍Python爬虫天气预报,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
正文
无聊不知道写点啥
运行结果
代码
import requests from fake_useragent import UserAgent from bs4 import BeautifulSoup def city_parse(url): headers={"User-Agent":UserAgent().random} response=requests.get(url=url,headers=headers).content.decode("utf-8") soup=BeautifulSoup(response,"html5lib") conMidtab=soup.find("div",attrs={"class":"conMidtab"}) table=conMidtab.find_all("table") for ta in table: trs=ta.find_all("tr")[2:] for index,tr in enumerate(trs): td=tr.find_all("td") if index ==0: city_td=td[1] else: city_td=td[0] city_temp=td[-2] city=list(city_td.stripped_strings)[0] temp=list(city_temp.stripped_strings)[0] print(city+"-最低温度:"+temp) def chioce_area(): print("请输入序号选择地区\n1.华北\n2.东北\n3.华东\n4.华中\n5.华南\n6.西北\n7.西南\n8.港澳台\n9.退出\n") num=int(input("请输入序号选择地区")) if num==1: url="http://www.weather.com.cn/textFC/hb.shtml" city_parse(url) if num ==2: url="http://www.weather.com.cn/textFC/db.shtml" city_parse(url) if num ==3: url = "http://www.weather.com.cn/textFC/hd.shtml" city_parse(url) if num ==4: url = "http://www.weather.com.cn/textFC/hz.shtml" city_parse(url) if num ==5: url = "http://www.weather.com.cn/textFC/hn.shtml" city_parse(url) if num ==6: url = "http://www.weather.com.cn/textFC/xb.shtml" city_parse(url) if num ==7: url = "http://www.weather.com.cn/textFC/xn.shtml" city_parse(url) if num ==8: url = "http://www.weather.com.cn/textFC/gat.shtml" city_parse(url) if num ==9: exit() if __name__=="__main__": flag=0 while flag==0 : chioce_area()
这篇关于Python爬虫天气预报的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门