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

 #encoding=utf-8
__author__ = 'heng'
#创建一个可以抓取输入网址源代码的GUI
from urllib2 import urlopen
import wx
#建立爬取网页的函数
def capture(event):
webpage = urlopen(the_URL.GetValue())
contents.SetValue(webpage.read())
webpage.close() #首先建立图形界面 app = wx.App()
win = wx.Frame(None,title = "The Editor ",size = (500,500))
bkg = wx.Panel(win)
searchButton = wx.Button(bkg,label = "Search") #设置搜索按键
#开始调用搜索函数
searchButton.Bind(wx.EVT_BUTTON,capture) the_URL = wx.TextCtrl(bkg) #创建URL的文本输入框
contents = wx.TextCtrl(bkg,style = wx.TE_MULTILINE | wx.HSCROLL) hbox = wx.BoxSizer()
hbox.Add(the_URL,proportion = 1,flag = wx.EXPAND)
hbox.Add(searchButton,proportion = 0,flag = wx.LEFT,border = 5) bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox,proportion = 0,flag = wx.EXPAND | wx.ALL,border = 5)
bbox.Add(contents,proportion = 1,flag = wx.EXPAND| wx.LEFT|wx.RIGHT|wx.BOTTOM,border = 5) bkg.SetSizer(bbox)
win.Show()
app.MainLoop()

运行效果图

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

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

05-27 18:09