获取 URL 的内容需要用到标准库 urllib 包, 其中的 request 模块.
- import urllib.request
- url='http://www.baidu.com'
- response=urllib.request.urlopen(url)
- string=response.read()
- html=string.decode('utf-8')
- print(HTML)
urlopen() 方法返回一个 < class 'http.client.HTTPResponse'>
即标准库 http 包里的对象, 该包是一个底层包, 由 request 模块调用.
read() 方法返回一个 < class 'bytes'>
字节对象转成 str 对象用 str.decode() 方法
将获取的 str 对象内容保存到 HTML 文件, 需用到程序内置的方法 open()
- f=open('lc.html','w')
- f.write(HTML)
- f.close()
open() 方法返回一个 < class '_io.TextIOWrapper'>
write() 方法是向文件对象写入 str 内容
最后要关闭文件对象
来源: http://www.bubuko.com/infodetail-3100055.html