Python抓取手機(jī)號(hào)歸屬地信息示例代碼
更新時(shí)間:2016年11月28日 11:44:36 作者:標(biāo)點(diǎn)符
之前看到一篇文章有提供手機(jī)號(hào)歸屬地?cái)?shù)據(jù)庫(kù)的下載,由于手機(jī)號(hào)號(hào)段一直在增加,所以提供的數(shù)據(jù)基本上隨時(shí)會(huì)過期,更理想的方法是從網(wǎng)上定期抓取其他站點(diǎn)維護(hù)的經(jīng)緯度信息。下面這篇文章就給大家介紹了如何利用Python抓取手機(jī)歸屬地信息,有需要的朋友們可以參考借鑒。
前言
本文給大家介紹的是利用Python抓取手機(jī)歸屬地信息,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的理解和學(xué)習(xí)很有幫助,以下為Python代碼,較為簡(jiǎn)單,供參考。
示例代碼
# -*- 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!"
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
python 讀取excel文件生成sql文件實(shí)例詳解
這篇文章主要介紹了python 讀取excel文件生成sql文件實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Python pandas如何獲取數(shù)據(jù)的行數(shù)和列數(shù)
這篇文章主要介紹了Python pandas如何獲取數(shù)據(jù)的行數(shù)和列數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python3.10.4激活venv環(huán)境失敗解決方法
這篇文章主要介紹了Python3.10.4激活venv環(huán)境失敗解決方法的相關(guān)資料,需要的朋友可以參考下2023-01-01

