使用Python对AQI历史数据进行爬取

 目标网站:天气后报网(www.tianqihoubao.com)

数据存储方式:csv文件本地存储

import requests
from lxml import etree
import csv
if __name__ == '__main__':
    url = 'http://www.tianqihoubao.com/aqi/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.204 Safari/537.36'
    }
    page_text = requests.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    city_list = tree.xpath('//div[@class="citychk"]/dl/dd')
    city_list = city_list[1:]
    for dd in city_list:
        #  获取城市名称和href和url
        city_name = dd.xpath('./a/text()')[0]
        city_name = city_name.encode('iso-8859-1').decode('gbk')
        # print(city_name)
        city_href = dd.xpath('./a/@href')[0]
        city_url = 'http://www.tianqihoubao.com' + city_href
        #  获取url响应数据
        city_AQI = requests.get(url=city_url, headers=headers).text
        city_tree = etree.HTML(city_AQI)
        city_aqi_list = city_tree.xpath('//div[@class="box p"]//li')[4:16]
        # print(city_aqi_list)
        for li in city_aqi_list:
            #  aqi每个月的url获取
            li_name = li.xpath('./a/@title')[0]
            li_href = li.xpath('./a/@href')[0]
            li_url = 'http://www.tianqihoubao.com' + li_href
            #  获取每个月aqi详细数据
            li_page = requests.get(url=li_url, headers=headers).text
            li_tree = etree.HTML(li_page)
            li_list = li_tree.xpath('//div[@class="api_month_list"]//tr')
            #  获取每天详细数据
            title = li_list[0]
            # print(li_list)
            list = [['日期\t','质量等级\t','AQI指数\t','PM2.5\t','PM10\t','So2\t','No2\t','Co\t','O3\n']]
            path = city_name + li_name + '.csv'
            with open(path, 'w', encoding='utf-8',newline='') as fp:
                for data in li_list[1:]:
                    #  获取详细数据
                    list_data= []
                    data_name = str(data.xpath('./td[1]/text()')[0])
                    data_name = data_name.strip()
                    list_data.append(data_name)
                    grade = str(data.xpath('./td[2]/text()')[0])
                    grade = grade.strip()
                    list_data.append(grade)
                    AQI = str(data.xpath('./td[3]/text()')[0])
                    AQI = AQI.strip()
                    list_data.append(AQI)
                    PM25 = data.xpath('./td[5]/text()')[0]
                    list_data.append(PM25)
                    PM10 = data.xpath('./td[6]/text()')[0]
                    list_data.append(PM10)
                    So2 = data.xpath('./td[7]/text()')[0]
                    list_data.append(So2)
                    No2 = data.xpath('./td[8]/text()')[0]
                    list_data.append(No2)
                    Co = data.xpath('./td[9]/text()')[0]
                    list_data.append(Co)
                    O3 = data.xpath('./td[10]/text()')[0]
                    list_data.append(O3)
                    list.append(list_data)
                    # fp.write(data_name)
                    # print('over!!')
                #  持久化存储
                rows = zip(list)
                writer = csv.writer(fp)
                for row in list:
                    writer.writerow(row)
                print(li_name, 'over!!!')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吗喽也是命

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值