Home > Backend Development > Python Tutorial > Sample code for Python to capture mobile phone number location information

Sample code for Python to capture mobile phone number location information

WBOY
Release: 2016-12-05 13:27:20
Original
1606 people have browsed it

前言

本文给大家介绍的是利用Python抓取手机归属地信息,文中给出了详细的示例代码,相信对大家的理解和学习很有帮助,以下为Python代码,较为简单,供参考。

示例代码

# -*- 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/.*&#63;/.*&#63;)" 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=".*&#63;">(.*&#63;)</a></div>',cityData)
 if provinceTemp:
  province = provinceTemp[0]
  city = re.findall('<meta name="description" content="(.*&#63;)共有',cityData)[0]
  tempData = re.findall('<div class="ab_menu.*&#63;</span>(.*&#63;) \(.*&#63;</div>.*&#63;<ul>(.*&#63;)</ul>',cityData)
  for temp in tempData:
  carrier = temp[0]
  numbers = re.findall('">(.*&#63;)</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!"
Copy after login

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template