-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path爬取空气质量标准.py
34 lines (26 loc) · 997 Bytes
/
爬取空气质量标准.py
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
import requests
from lxml import etree
def spider():
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
url = 'https://www.tianqi.com/air/'
response = requests.get(url, headers=headers)
selector = etree.HTML(response.text)
all_tr = selector.xpath('//*[@id="aqs-unit-fixed"]/div[2]/table/tbody/tr')
title1 = all_tr[0].xpath('th[1]/text()')[0]
title2 = all_tr[0].xpath('th[2]/text()')[0]
title3 = all_tr[0].xpath('th[3]/text()')[0]
item = [title1, title2, title3]
info = []
info.append(item)
all_tr.remove(all_tr[0])
for tr in all_tr:
t1 = tr.xpath('td[1]/text()')[0]
t2_1 = tr.xpath('td[2]/text()')[0]
t2_2 = tr.xpath('td[2]/em/text()')[0]
t2 = t2_1 + '=>' + t2_2
t3 = tr.xpath('td[3]/text()')[0]
item = [t1, t2, t3]
info.append(item)
return info
if __name__ == '__main__':
spider()