簡(jiǎn)單的一個(gè)例子,是以前用Dephi寫的,前不久剛實(shí)現(xiàn)了一個(gè)在Python中使用Delphi控件來(lái)編寫界面程序,于是趁熱寫一個(gè)類似的的查詢方案。
本實(shí)例是通過(guò)www.ip138.com這個(gè)網(wǎng)站來(lái)查詢的,這里需要的幾個(gè)知識(shí)點(diǎn),就是用Python模擬網(wǎng)頁(yè)提交數(shù)據(jù),獲得數(shù)據(jù)返回信息,以及對(duì)返回的Html信息進(jìn)行解析,模擬Http提交,Python自帶有一個(gè)urllib和urllib2這兩個(gè)庫(kù),相當(dāng)方便,只是奇怪,為什么不將兩個(gè)庫(kù)合并成一個(gè),這樣來(lái)的更方便。然后就是窗體了,窗體還是用我之前寫的一個(gè)Python模塊DxVcl,就是可以在Python中調(diào)用Delphi界面控件的一個(gè)模塊庫(kù)。下面就貼上代碼,相當(dāng)簡(jiǎn)單的!
#-*-coding: gb2312 -*-import urllib,urllib2,HTMLParserfrom 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 = Falsedef handle_data(self,data):if self._isInTd:self._retdata.append(data)class MainForm(Form):def__init__(self,Owner):self.Caption ='查詢手機(jī)歸屬地'self.Position =5self.BorderStyle =3self.Width =303self.Height =375self.lbl = Label(self)self.lbl.SetProps(Parent = self,Caption ='手機(jī)號(hào)碼')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.Button1Clickself.Memo1 = Memo(self)self.Memo1.Parent = selfself.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 ='查詢手機(jī)歸屬'f = MainForm(Application)f.Show()Application.Run()if__name__=='__main__':main() 運(yùn)行之后的界面
以上所述是小編給大家介紹的Python手機(jī)號(hào)碼歸屬地查詢代碼,希望對(duì)大家有所幫助!




















