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爬取网络图片

本文实例为大家分享了Python爬取网络图片的具体代码,供大家参考,具体内容如下 代码: import urllib import urllib.request import re...

python3使用urllib模块制作网络爬虫

urllib urllib模块是python3的URL处理包 其中: 1、urllib.request主要是打开和阅读urls 个人平时主要用的1: 打开对应的URL:urllib.re...

Python爬取读者并制作成PDF

学了下beautifulsoup后,做个个网络爬虫,爬取读者杂志并用reportlab制作成pdf.. crawler.py 复制代码 代码如下: #!/usr/bin/env pyth...

Python爬虫:url中带字典列表参数的编码转换方法

平时见到的url参数都是key-value, 一般vlaue都是字符串类型的 如果有幸和我一样遇到字典,列表等参数,那么就幸运了 python2代码 import json from...

Python网络爬虫与信息提取(实例讲解)

Python网络爬虫与信息提取(实例讲解)

课程体系结构: 1、Requests框架:自动爬取HTML页面与自动网络请求提交 2、robots.txt:网络爬虫排除标准 3、BeautifulSoup框架:解析HTML页面 4、R...