import requests
from lxml import etree
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)
path = city_name + li_name + '.txt'
with open(path, 'w', encoding='utf-8') as fp:
for data in li_list[1:]:
data_name = data.xpath('./td[1]/text()')
data_name = list(filter(None,data_name))
print(data_name)
grade = data.xpath('./td[2]/text()')
grade = list(filter(None, grade))
print(grade)
AQI = data.xpath('./td[3]/text()')
AQI = list(filter(None, AQI))
print(AQI)
PM25 = data.xpath('./td[5]/text()')
print(PM25)
PM10 = data.xpath('./td[6]/text()')
print(PM10)
So2 = data.xpath('./td[7]/text()')
print(So2)
No2 = data.xpath('./td[8]/text()')
print(No2)
Co = data.xpath('./td[9]/text()')
print(Co)
O3 = data.xpath('./td[10]/text()')
print(O3)
-