如何使用urllib
# -*- coding:utf-8 -*-
#
import Tkinter
import urllib
class Window:
def __init__(self, root):
self.root = root
self.entryUrl = Tkinter.Entry(root)
self.entryUrl.place(x = 5, y = 15)
self.get = Tkinter.Button(root, text = '下载页面', command = self.Get)
self.get.place(x = 120, y = 15)
self.edit = Tkinter.Text(root)
self.edit.place(y = 50)
def Get(self):
url = self.entryUrl.get()
page = urllib.urlopen(url)
data = page.read()
self.edit.insert(Tkinter.END, data)
page.close()
root = Tkinter.Tk()
window = Window(root)
root.minsize(600, 480)
root.mainloop()
试一下访问[示例网址](http://www.163.com)
什么是urllib
使用Python的urllib模块可以创建一个简单的访问网站的脚本,获取指定的页面。
如果使用GUI库中显示HTML的组件,还可以制作一个简单的Python Web浏览器。
使用urllib进行简单地访问、下载页面内容。