File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ # encoding: utf-8
2+ __author__ = 'zhanghe'
3+
4+
5+ import requests
6+ import re
7+ import json
8+
9+ # 入口页的url
10+ url = 'http://www.58.com/changecity.aspx'
11+
12+ # 伪装成浏览器
13+ header = {
14+ 'Host' : 'www.58.com' ,
15+ 'Referer' : 'http://sh.58.com/' ,
16+ 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'
17+ }
18+
19+ s = requests .session ()
20+
21+
22+ def get_city_list ():
23+ """
24+ 获取城市列表
25+ """
26+ response = s .get (url , headers = header )
27+ html = response .text
28+ rule = '<a href="http://.*?.58.com/" onclick="co\(\' (.*?)\' \)">(.*?)</a>'
29+ city_list = re .compile (rule , re .S ).findall (html )
30+ city = {}
31+ for item in city_list :
32+ city [item [0 ]] = item [1 ]
33+ print json .dumps (city , indent = 4 ).decode ('raw_unicode_escape' )
34+
35+
36+ if __name__ == '__main__' :
37+ get_city_list ()
You can’t perform that action at this time.
0 commit comments