关于urllib库
[
这个是Python自带的库,不需要独立安装的库。
一个基本测试代码:
import urllib.request
# 定义一个URL,要访问的地址
url =‘http://www.baidu.com’
# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(url)
print(response)
# 获取响应中的页面的源码
content = response.read()
print(content) #输出内容
输出:b’ \n\n\n …
b表示字节形式的二进制数据
# 将二进制数据转为字符串–解码
# decode() 方法,字符串编码charset=‘utf-8’
text = content.decode(“utf-8”)
print(text)
输出字符串HTML,现在可以看到汉字了。