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

yipeiwu_com6年前Python爬虫

前言

本文给大家介绍的是利用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支持多线程的爬虫实例

python是支持多线程的, 主要是通过thread和threading这两个模块来实现的,本文主要给大家分享python实现多线程网页爬虫 一般来说,使用线程有两种模式, 一种是创建线...

python实现博客文章爬虫示例

复制代码 代码如下:#!/usr/bin/python#-*-coding:utf-8-*-# JCrawler# Author: Jam <810441377@qq.com>...

python爬虫 urllib模块发起post请求过程解析

python爬虫 urllib模块发起post请求过程解析

urllib模块发起的POST请求 案例:爬取百度翻译的翻译结果 1.通过浏览器捉包工具,找到POST请求的url 针对ajax页面请求的所对应url获取,需要用到浏览器的捉包工具。...

使用Python3编写抓取网页和只抓网页图片的脚本

最基本的抓取网页内容的代码实现: #!/usr/bin/env python from urllib import urlretrieve def firstNonBl...

python爬虫租房信息在地图上显示的方法

python爬虫租房信息在地图上显示的方法

本人初学python是菜鸟级,写的不好勿喷。 python爬虫用了比较简单的urllib.parse和requests,把爬来的数据显示在地图上。接下里我们话不多说直接上代码: 1.安装...