美文网首页Python
【python 学习】python 爬取天气预报

【python 学习】python 爬取天气预报

作者: 鸦言 | 来源:发表于2020-06-29 11:38 被阅读0次

    天气预报爬取的python教程有很多,但时过境迁,教程上的有些办法已经不适用了。这里介绍我2020年6月29日爬取信息所用的代码。

    天气预报的网址的获得

    这里我爬取的目标网址为2345天气预报

    1. 切换到历史天气选项卡,选择目标区域.
    2. F12打开开发者模式,找到Network选项卡。
    3. 在网页界面调整月份,开发者模式界面的Name对话框中会有一条新的记录,点开它。
    4. 打开“Headers”选项卡,记住(复制/保存)"Request URL: "后面的链接(这就是爬取数据所用的链接)。

    获得链接中的所有内容

    import requests  
    years = [2017, 2018, 2019, 2020] #所要爬取的年份
    months = []
    for year in years:
        todo_urls = [f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=71920&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month+1}' # 刚刚保存的链接,将年和月的数字分别替换为“{year}”和“{year}”
                     for year in years
                     for month in range(12)]
    datas = [] # 将所有的数据全部保存到datas中
    for url in todo_urls:
        r = requests.get(url)
        if r.status_code != 200:
            raise Exception()
        data = r.text
        datas.append(data)
    

    数据的读取与保存

    先用demjson把还是乱码的数据转化为能看懂的格式。

    import demjson #使用demjson模块读取数据,没有的话pip install一下。
    t = open("i.txt", "w") #将所有信息写入到txt中,方便整理。
    for data in datas:
        i = demjson.decode(data)
        j = str(i) #将字典转化为文本
        j = j.replace("</tr>", "\n") #原文本中的“\n”似乎在txt中不起作用,重新手动构建一些换行符。
        t.write(j)
    t.close()
    

    数据的整理

    因为不知道有什么好用的模块,所以只好选择手动整理,看起来很麻烦,但是如果熟练掌握复制粘贴也花费不了太多时间。

    text = open("i.txt", "r")
    tab = open("i.csv", "w")
    tab.write("日期")
    tab.write(",")
    tab.write("最高气温(℃)")
    tab.write(",")
    tab.write("最低气温(℃)")
    tab.write(",")
    tab.write("天气")
    tab.write(",")
    tab.write("风")
    tab.write(",")
    tab.write("空气指数")
    tab.write(",")
    tab.write("空气质量")
    for line in text.readlines():
        line = str(line)
        if "周" in line:
            tab.write("\n")
            a, b, c = line.partition("<td>")
            a, b, e = c.partition(" 周")
            tab.write(a)
            tab.write(",")
            a, b, c = e.partition(';\">')
            a, b, e = c.partition("°")
            tab.write(a)
            tab.write(",")
            a, b, c = e.partition(" >")
            a, b, e = c.partition("°")
            tab.write(a)
            tab.write(",")
            a, b, c = e.partition("<td>")
            a, b, e = c.partition("</td>")
            tab.write(a)
            tab.write(",")
            a, b, c = e.partition("<td>")
            a, b, e = c.partition("</td>")
            tab.write(a)
            tab.write(",")
            a, b, c = e.partition('">')
            a, b, e = c.partition(" ")
            tab.write(a)
            tab.write(",")
            a, b, c = e.partition('</span>')
            tab.write(a)
            tab.write(",")
    tab.close()
    text.close()
    

    整理完毕,可以看一下整理的效果

    因为我所关注的地区没有2017年4月23日及之前的信息,所以记录是从4月24日开始的。


    image.png

    相关文章

      网友评论

        本文标题:【python 学习】python 爬取天气预报

        本文链接:https://www.haomeiwen.com/subject/avcofktx.html