Python语言实现手机号归属地的方法

现在骗子越来越猖獗,大家每天接到的陌生骚扰电话不计其数,现在手机有个功能能显示手机来电的归属地,最近自己有空余时间,在网上查了下,发现使用的是Python 开发( http://www.maiziedu.com/course/python/ 语言编写的,在 Python 中使用 Delphi 空间来编写界面程序,于是自己趁热写了一个类型的查询方案。
    本实例是通过 www.ip138.com 这个网站来查询的,这里需要的几个知识点,就是用 Python 模拟网页提交数据,获得数据返回信息,以及对返回的  Html 信息进行解析,模拟 Http 提交 ,Python 自带有一个 urllib urllib2 这两个库,相当方便,只是奇怪,为什么不将两个库合并成一 个,这样来的更方便。然后就是窗体了,窗体还是用我之前写的一个 Python 模块 DxVcl ,就是可以在 Python 中调用 Delphi 界面控件的一个模 块库。下面就贴上代码,相当简单的!
#-*-coding: gb2312 -*-
import urllib,urllib2,HTMLParser
from DxVcl import*
class MyParser(HTMLParser.HTMLParser):
    def reset(self):
        self._isInTd = False        
        self._retdata = []
        HTMLParser.HTMLParser.reset(self)
    def handle_starttag(self,tag,attris):
        self._isInTd = tag =='td'
    def handle_endtag(self,tag):
        if self._isInTd:
            self._isInTd = False
    def handle_data(self,data):
        if self._isInTd:
            self._retdata.append(data)
class MainForm(Form):
    def__init__(self,Owner):
        self.Caption =' 查询手机归属地 '
        self.Position =5
        self.BorderStyle =3
        self.Width =303
        self.Height =375
        self.lbl = Label(self)
        self.lbl.SetProps(Parent = self,Caption =' 手机号码 ')
        self.lbl.SetBounds(16,8,60,13)
        self.EdtPhone = Edit(self)
        self.EdtPhone.SetProps(Parent = self,Text ='')
        self.EdtPhone.SetBounds(77,3,121,21)
        self.Button1 = Button(self)
        self.Button1.SetProps(Parent = self,Caption =' 查询 ')
        self.Button1.SetBounds(204,1,75,25)
        self.Button1.OnClick = self.Button1Click
        self.Memo1 = Memo(self)
        self.Memo1.Parent = self
        self.Memo1.SetBounds(16,32,263,297)   
    def Button1Click(self,Sender):
        postdata = urllib.urlencode([('action','mobile'),('mobile',self.EdtPhone.Text)])
        req = urllib2.Request('http://www.ip138.com:8080/search.asp')
        fd = urllib2.urlopen(req,postdata)
        h = fd.read()
        my = MyParser()
        my.feed(h)
        self.Memo1.Lines.Clear()
        for data in my._retdata:
            self.Memo1.Lines.Add(data)
def main():
    FreeConsole()
    Application.Initialize()
    Application.Title =' 查询手机归属 '
    f = MainForm(Application)
    f.Show()
    Application.Run()
if__name__=='__main__':
main()
运行之后的界面
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值