日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 手机号码归属 ip地址查询

發(fā)布時(shí)間:2023/12/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 手机号码归属 ip地址查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python腳本實(shí)例 包括xml,json,http,編碼

Posted in?Python?-?4 五月 2009?- No comment

這幾天在研究python相關(guān)的一些東西,其中涉及的內(nèi)容包括:

xml的解析,json的解析,urllib的使用,urllib的使用,python中中文編碼的問題等

項(xiàng)目托管在Google上,地址是http://code.google.com/p/ipmobile/source/checkout

當(dāng)然一切都來源于實(shí)際工作嘛,首先引入緣由,情景是這樣的:

有一個(gè)Excel表格,有注冊日期,用戶ID,注冊IP,注冊手機(jī)號這幾個(gè)字段,
但現(xiàn)在需要獲取注冊IP所在的地址 以及手機(jī)號所屬地區(qū)等信息,
并根據(jù)這些信息進(jìn)行分類統(tǒng)計(jì)等工作

當(dāng)然如果在沒有編碼人員介入的情況下,
需要一個(gè)個(gè)的到一些網(wǎng)絡(luò)上的頁面上查出地址的信息,然后填到excel里,
而這完全是重復(fù)勞動(dòng),完全可以抽象并做出系統(tǒng),在沒有人力進(jìn)行系統(tǒng)開發(fā)的情況下,
可以使用簡單的腳本進(jìn)行替代,
本文中,本人使用python腳本進(jìn)行了相關(guān)研究和實(shí)驗(yàn)性的工作.用Python寫出的代碼的可讀性非常的高的
首先是一個(gè)簡單的例子使用過的是有道的接口獲取IP和手機(jī)號碼的地址的

?

#!/usr/bin/env python
#coding=utf-8

"""
getinfo.py
Created by Luke on 2009-04-29.
Copyright (c) 2009 Alibaba. All rights reserved.
"""


import?urllib
from?xml2dict?import?XML2Dict
import?codecs


def?gbk2utf8(xml):
????return?xml.decode("gbk").encode("utf8").replace('gbk',?'utf-8');

def?get_ip_info(ip):
????url="http://www.yodao.com/smartresult-xml/search.s?type=ip&q=%s"%ip
????return??gbk2utf8(urllib.urlopen(url).read())

def?get_mobile_info(mobile):
????url="http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=%s"%mobile
????return?gbk2utf8(urllib.urlopen(url).read())


def?main():
????fr?=?open('x.csv')
????wr?=?codecs.open('y.csv','w','gbk')
????xml?=?XML2Dict()
????for?line?in?fr.readlines():
????????line?=?line.rstrip('n')
????????items?=?line.split(',')
????????ip_item?=?items[2]
????????mobile_item?=?items[3]
????????ip_dict?=?xml.fromstring(get_ip_info(ip_item))
????????mobile_dict?=?xml.fromstring(get_mobile_info(mobile_item))
????????new_line=''
????????if?ip_dict.smartresult!=None:
????????????new_line?=?items[0]+','+items[1]+','+items[2]+','
?????????????????????????????????????????+ip_dict.smartresult.product.location.encode("gbk")
????????else:
????????????new_line?=?items[0]+','+items[1]+','+items[2]+','
????????if?mobile_dict.smartresult!=None:
????????????new_line?=?new_line+','+items[3]+','
?????????????????????????????????????????+mobile_dict.smartresult.product.location.encode("gbk")+'n'
????????else:
????????????new_line?=?new_line+','+items[3]+','+'n'
????????print?new_line.decode("gbk")
????????wr.write(new_line.decode("gbk"))
????fr.close();
????wr.close();


if?__name__?==?'__main__':
????main()

這段代碼從有道那獲得的結(jié)果是xml編碼格式為gbk的,所以在編碼上我將其先轉(zhuǎn)換成utf8的,然后再通過xml2dict將其轉(zhuǎn)換成Python的字典結(jié)構(gòu),這樣我就可以直接讀取節(jié)點(diǎn)的值了.
這段代碼還使用了urllib模塊還有讀文件寫文件的相關(guān)知識點(diǎn),總體來說還是很簡單的,主要就是字符串的拼接了,這里的文件格式都是csv的很容易就可以編程excel的格式

但是問題來了,有的IP地址在有道這里查不出來,而在ip138這個(gè)網(wǎng)站上可以查出來,后來試了好多,發(fā)現(xiàn)IP138的數(shù)據(jù)庫的數(shù)據(jù)貌似全一些,但是IP138沒有提供任何數(shù)據(jù)讀取的API,那怎么辦呢?嘿嘿,那就用最那個(gè)的辦法了,在腳本里解析使用正則表達(dá)式處理html,解析出想要的結(jié)果,代碼如下:

"""
ip138poster.py
Created by Luke on 2009-05-04.
Copyright (c) 2009 Alibaba. All rights reserved.
"""


import?httplib,urllib;??#加載模塊
import?re

class?Ip138Poster:

????def?__init__(self):
????????#定義一些文件頭
????????self.headers?=?{"Content-Type":"application/x-www-form-urlencoded",
????????????????????"Connection":"Keep-Alive","Referer":"http://www.ip138.com/ips.asp"};
????????#與網(wǎng)站構(gòu)建一個(gè)連接
????????self.conn?=?httplib.HTTPConnection("www.ip138.com");

????def?post(self,ip_addr):
????????#定義需要進(jìn)行發(fā)送的數(shù)據(jù)
????????params?=?urllib.urlencode({'ip':ip_addr,'action':'2'});
????????#開始進(jìn)行數(shù)據(jù)提交 ??同時(shí)也可以使用get進(jìn)行
????????self.conn.request(method="POST",url="/ips8.asp",
????????????????????????????????????????????body=params,headers=self.headers);
????????#返回處理后的數(shù)據(jù)
????????response?=?self.conn.getresponse();
????????#判斷是否提交成功
????????if?response.status?==?200:
????????????s=response.read()
????????????print?s.decode("gbk")
????????????lis?=?re.findall(r"(?<=<li>).*?(?=</li>)",s)
????????????return?lis

????def?get_main_data(self,ip_addr):
????????##下面的十六進(jìn)制的值為"本站主數(shù)據(jù):"的unicode
????????self.main_data_parttern?=?r"(?<=xe6x9cxacxe7xabx99xe4xb8xbbxe6x95xb0xe6x8dxaexefxbcx9a).*"
????????list?=?self.post(ip_addr)
????????if?len(list)>0:
????????????main_data?=?re.findall(self.main_data_parttern,list[0].decode("gbk").encode("utf8"))
????????if?len(main_data)>0:
????????????ret_value?=?main_data[0]
????????return?ret_value.decode("utf8")



????def?__del__(self):
????????#關(guān)閉連接
????????self.conn.close()



##由于需要匹配中文,所以需要轉(zhuǎn)換,然后再匹配
def?UTF2Hex(s):
????temp?=?s.encode("UTF-8").encode("hex")
????line?=?""
????for?i?in?range(0,len(temp)-1,2):
????????line +=?"\x"?+ temp[i]?+ temp[i+1]
????return?line

def?GBK2Hex(s):
????temp?=?s.encode("GBK").encode("hex")
????line?=?""
????for?i?in?range(0,len(temp)-1,2):
????????line +=?"\x"?+ temp[i]?+ temp[i+1]
????return?line


if?__name__?==?'__main__':
????poster?=?Ip138Poster()
????print?poster.get_main_data("121.0.29.231")

?這里我還提供一個(gè)json的可選方案,可惜的是目前我沒有發(fā)現(xiàn)國內(nèi)的可以使用json協(xié)議獲取ip地址信息的API(SOAP的萬網(wǎng)好像有個(gè)),所以IP的API是使用國外的,手機(jī)號碼信息查詢國內(nèi)是有API的,可以直接使用,代碼非常非常簡單,如下:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
# encoding: utf-8
"""
locatingsystem.py

Created by Luke on 2009-05-04.
Copyright (c) 2009 Alibaba.com. All rights reserved.
"""


import?sys
import?os
import?urllib
import?json

class?LocatingSystem:
????def?get_ip_json(self,ip):
????????url="http://iplocationtools.com/ip_query.php?ip=%s&output=json"%ip
????????return?json.read(urllib.urlopen(url).read())

????def?get_mobile_json(self,mobile):
????????url="http://api.showji.com/locating/?m=%s&output=json"%mobile
????????return?json.read(urllib.urlopen(url).read())

def?main():
????ls?=?LocatingSystem()
????print?ls.get_ip_json("134.34.54.56")
????print?ls.get_mobile_json("13819127490")


if?__name__?==?'__main__':
????main()

總結(jié)

以上是生活随笔為你收集整理的python 手机号码归属 ip地址查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。