爬取指定网页的源代码显示在GUI中

建立一个GUI图形界面用来用来输入网址和代码显示的区域

 1 #encoding=utf-8
 2 __author__ = 'heng'
 3 #创建一个可以抓取输入网址源代码的GUI
 4 from urllib2 import urlopen
 5 import wx
 6 #建立爬取网页的函数
 7 def capture(event):
 8     webpage = urlopen(the_URL.GetValue())
 9     contents.SetValue(webpage.read())
10     webpage.close()
11 
12 #首先建立图形界面
13 
14 app = wx.App()
15 win = wx.Frame(None,title = "The Editor ",size = (500,500))
16 bkg = wx.Panel(win)
17 searchButton = wx.Button(bkg,label = "Search")   #设置搜索按键
18 #开始调用搜索函数
19 searchButton.Bind(wx.EVT_BUTTON,capture)
20 
21 the_URL = wx.TextCtrl(bkg)                        #创建URL的文本输入框
22 contents = wx.TextCtrl(bkg,style = wx.TE_MULTILINE | wx.HSCROLL)
23 
24 hbox = wx.BoxSizer()
25 hbox.Add(the_URL,proportion = 1,flag = wx.EXPAND)
26 hbox.Add(searchButton,proportion = 0,flag = wx.LEFT,border = 5)
27 
28 bbox = wx.BoxSizer(wx.VERTICAL)
29 bbox.Add(hbox,proportion = 0,flag = wx.EXPAND | wx.ALL,border = 5)
30 bbox.Add(contents,proportion = 1,flag = wx.EXPAND| wx.LEFT|wx.RIGHT|wx.BOTTOM,border = 5)
31 
32 bkg.SetSizer(bbox)
33 win.Show()
34 app.MainLoop()

运行效果图

输入要爬取的网页之后点击search就可以开始爬取指定的网页。

 

posted @ 2015-04-19 20:51  xiaoli2018  阅读(464)  评论(0编辑  收藏  举报