前言
本文給大家介紹的是利用Python抓取手機歸屬地信息,文中給出了詳細的示例代碼,相信對大家的理解和學習很有幫助,以下為Python代碼,較為簡單,供參考。
示例代碼
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
|
# -*- coding:utf-8 -*- import requests,re o = open ( 'data.txt' , 'a' ) e = open ( 'error.txt' , 'a' ) baseUrl = 'http://www.iluohe.com/' r = requests.get( 'http://www.iluohe.com/all.shtml' ,) links = re.findall( '<a href="(city/.*?/.*?)" target' ,r.content.decode( "gbk" ).encode( "utf-8" )) for link in links: link = baseUrl + link cityData = requests.get(link) if cityData.status_code > = 300 : e.writelines(link + "\n" ) else : cityData = cityData.content.decode( "gbk" ).encode( "utf-8" ) provinceTemp = re.findall( '<div class="NameSzu"><a href=".*?">(.*?)</a></div>' ,cityData) if provinceTemp: province = provinceTemp[ 0 ] city = re.findall( '<meta name="description" content="(.*?)共有' ,cityData)[ 0 ] tempData = re.findall( '<div class="ab_menu.*?</span>(.*?) \(.*?</div>.*?<ul>(.*?)</ul>' ,cityData) for temp in tempData: carrier = temp[ 0 ] numbers = re.findall( '">(.*?)</a></li>' ,temp[ 1 ]) for number in numbers: text = number + "," + carrier + "," + city + "," + province o.writelines(text) o.writelines( '\n' ) else : e.writelines(link + "\n" ) o.close() print "over!" |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:http://www.biaodianfu.com/python-get-mobile-area.html