ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python爬虫天气预报

2021-05-22 23:02:16  阅读:177  来源: 互联网

标签:city www Python 爬虫 parse url num 天气预报 com


正文

无聊不知道写点啥

运行结果

代码

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()

标签:city,www,Python,爬虫,parse,url,num,天气预报,com
来源: https://blog.csdn.net/gushuiwuqiu/article/details/117172764

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有