在Python中读取网页文档,你可以使用`requests`库或者`urllib`库。以下是使用这两个库的简单示例:
使用`requests`库
import requests发送一个HTTP GET请求,获取网页内容response = requests.get('http://example.com')打印网页内容print(response.text)
使用`urllib`库

import urllib.request打开一个URLwith urllib.request.urlopen('http://www.python.org') as response:读取网页内容html = response.read()打印网页内容print(html)
以上代码示例展示了如何使用`requests`和`urllib`库来获取网页内容。`requests`库更加简洁易用,而`urllib`库是Python标准库的一部分,无需额外安装。
