用Python查询手机号码归属地

#! /usr/bin/python
#  -*- coding: utf8 -*-  

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()


你可能感兴趣的:(用Python查询手机号码归属地)