-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01.py
More file actions
52 lines (43 loc) · 1.46 KB
/
Copy path01.py
File metadata and controls
52 lines (43 loc) · 1.46 KB
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
import urllib.request
import bs4
import re
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'AD_RS_COOKIE=20080917',
'Host': 'www.stats.gov.cn',
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/'
req = urllib.request.Request(url, headers=headers)
res = urllib.request.urlopen(req).read()
soup = bs4.BeautifulSoup(res, 'html.parser')
list_province = soup.select(".provincetr")
tmpPrivinceList = []
for tr in list_province:
a_list = tr.select('a')
for a in a_list:
tmpPrivinceList.append((
a.get('href'),
a.text
))
tmpCityList = []
for href, name in tmpPrivinceList:
tmpUrl = url + href
req = urllib.request.Request(tmpUrl, headers=headers)
res = urllib.request.urlopen(req).read()
soup = bs4.BeautifulSoup(res, 'html.parser')
list_city = soup.select(".citytr")
for city in list_city:
a_list = city.select('a')
for i in range(0, len(a_list), 2):
tmpCityList.append((
a_list[i].get('href'),
a_list[i].text,
a_list[i + 1].text
))
break
print(tmpCityList)