• python爬虫案例-爬取山东各城市近两年的天气情况(附带源码)


    咱们先看爬取的数据

    在这里插入图片描述

    话不多说

    直接开始代码的编写

    首先导入第三方库

    import requests
    import json
    import parsel # 解析数据 第三方模块
    # 导入内置模块来保存文件
    import csv
    
    • 1
    • 2
    • 3
    • 4
    • 5

    打开csv文件写入我们要爬取的数据表头

    with open('山东各个城市历史天气.csv', mode='a', newline='', encoding='utf-8')as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['日期','最高温','最低温','天气','风力风向','空气质量指数','城市'])
    
    
    • 1
    • 2
    • 3
    • 4

    确定爬取的网站,然后拿到山东所有城市的ID

    遍历爬取所有城市的天气

    最后保存到本地

    city_list=[54823,54734,54714,54736,54906,54915,54806,54938,54857,54945,54827,54843,54774,54765,54830,58024]
    for city in city_list:
    
        for year in range(2021,2023):
            for month in range(1,13):
                url=f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'
    
                # 进行请求并接收
                response = requests.get(url = url)
                #返回 说明接收正常
                #可以直接获取,不需要反爬措施
    
                # 获取数据,用json字典形式储存
                json_data = response.json()
    
    
                # 解析数据
                html_data= json_data['data']
                # .history-table tr
                selector=parsel.Selector(html_data)
                trs = selector.css('.history-table tr')[1:] # 不需要第一行
                for tr in trs:
                    #::text:获取标签文本内容
                    # .getall():  获取所有的td标签
                    td = tr.css( 'td::text')
                    if city==54823:
                        td.append('济南')
                    elif city==54734:
                        td.append('滨州')
                    elif city==54714:
                        td.append('德州')
                    elif city==54736:
                        td.append('东营')
                    elif city==54906:
                        td.append('菏泽')
                    elif city==54915:
                        td.append('济宁')
                    elif city==54806:
                        td.append('聊城')
                    elif city==54938:
                        td.append('临沂')
                    elif city==54857:
                        td.append('青岛')
                    elif city==54945:
                        td.append('日照')
                    elif city==54827:
                        td.append('泰安')
                    elif city==54843:
                        td.append('潍坊')
                    elif city==54774:
                        td.append('威海')
                    elif city==54765:
                        td.append('烟台')
                    elif city==54830:
                        td.append('淄博')
                    elif city==58024:
                        td.append('枣庄')
    
                    print(td)
                    with open('山东各个城市历史天气.csv',mode='a',newline='',encoding='utf-8')as f:
                        csv_writer=csv.writer(f)
                        csv_writer.writerow(td)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    全部源码放在这

    # coding:utf-8
    # author:songxin
    # email:812067265@qq.com
    # -- coding: utf-8 --
    '''目标:爬取天气后报网山东各个城市天气'''
    #导入requests模块
    #在Terminal中输入pip install requests
    
    import requests
    import json
    import parsel # 解析数据 第三方模块
    # 导入内置模块来保存文件
    import csv
    
    with open('山东各个城市历史天气.csv', mode='a', newline='', encoding='utf-8')as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['日期','最高温','最低温','天气','风力风向','空气质量指数','城市'])
    
    # 确定目标网站 2345天气网
    #先爬取山东济南历史天气
    city_list=[54823,54734,54714,54736,54906,54915,54806,54938,54857,54945,54827,54843,54774,54765,54830,58024]
    for city in city_list:
    
        for year in range(2021,2023):
            for month in range(1,13):
                url=f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'
    
                # 进行请求并接收
                response = requests.get(url = url)
                #返回 说明接收正常
                #可以直接获取,不需要反爬措施
    
                # 获取数据,用json字典形式储存
                json_data = response.json()
    
    
                # 解析数据
                html_data= json_data['data']
                # .history-table tr
                selector=parsel.Selector(html_data)
                trs = selector.css('.history-table tr')[1:] # 不需要第一行
                for tr in trs:
                    #::text:获取标签文本内容
                    # .getall():  获取所有的td标签
                    td = tr.css( 'td::text')
                    if city==54823:
                        td.append('济南')
                    elif city==54734:
                        td.append('滨州')
                    elif city==54714:
                        td.append('德州')
                    elif city==54736:
                        td.append('东营')
                    elif city==54906:
                        td.append('菏泽')
                    elif city==54915:
                        td.append('济宁')
                    elif city==54806:
                        td.append('聊城')
                    elif city==54938:
                        td.append('临沂')
                    elif city==54857:
                        td.append('青岛')
                    elif city==54945:
                        td.append('日照')
                    elif city==54827:
                        td.append('泰安')
                    elif city==54843:
                        td.append('潍坊')
                    elif city==54774:
                        td.append('威海')
                    elif city==54765:
                        td.append('烟台')
                    elif city==54830:
                        td.append('淄博')
                    elif city==58024:
                        td.append('枣庄')
    
                    print(td)
                    with open('山东各个城市历史天气.csv',mode='a',newline='',encoding='utf-8')as f:
                        csv_writer=csv.writer(f)
                        csv_writer.writerow(td)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
  • 相关阅读:
    07 流量回放实现自动化回归测试
    Linux常用命令
    论如何在Android中还原设计稿中的阴影
    Polygon zkEVM Binary状态机
    Java 8 stream的详细用法
    优维产品最佳实践第14期:让重要告警能有序跟进,最终根治
    操作系统,计算机网络,数据库刷题笔记2
    【Java】cron表达式
    力扣刷题之求两数之和
    Windows系统加密
  • 原文地址:https://blog.csdn.net/pythonuser1/article/details/126194129