Python sample code to grab the phone number home location information
- 2020-05-17 05:48:56
- OfStack
preface
This paper introduces the use of Python to grab mobile phone ownership information, and gives a detailed sample code, I believe it is very helpful for you to understand and learn, the following is the Python code, relatively simple, for reference.
The sample code
# -*- 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="(.*?) A total of ',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!"
conclusion
The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.