返回Python抓......登陆

Python抓取手机号归属地信息示例代码

巴扎黑2017-01-09 11:38:55307

前言

本文给大家介绍的是利用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/.*?/.*?)" 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!"

更多关于Python抓取手机号归属地信息示例代码请关注PHP中文网(www.php.cn)其他文章!   


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送