在使用request爬取网页时,避不开编码问题。
爬取网页代码诸如以下所示:
response = requests.get(url)
到这个地方,存在两种方法提取信息,一种是response.text,另一种是response.content
两者区别:
response.text返回的是一个unicode型的文本数据,只有文本信息
response.content返回的是bytes型的二进制数据,包括图片、文本、文件信息等
两种方法转化为可用的方法如下:
# 方法1
response = requests.get(url)
print(response.content.decode('utf-8'))
# 方法2
response = requests.get(url)
response.encoding = 'utf8'
print(response.text)
如果控制台输出还是有问题,那说明是pycharm等IDE的设置问题。
1108

被折叠的 条评论
为什么被折叠?



