对于Python3爬虫抓取网页中文出现输出乱码
import urllib.request response = urllib.request.urlopen('http://www.12306.cn/mormhweb/') html = response.read()
print(html)上面的代码正常但是运行的时候结果遇到中文会以\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80代替,这是一种byte字节。python 3输出位串,而不是可读的字符串,需要对其进行转换使用str(string[, encoding])对数组进行转换
str(response.read(),'utf-8')
import urllib.request response = urllib.request.urlopen('http://www.12306.cn/mormhweb/')
html =str(response.read(),'utf-8') print(html)
这样就解决了中文不能输出问题,效果如下
本文介绍如何解决Python3爬虫抓取网页时遇到的中文乱码问题。通过使用正确的解码方式,将抓取到的byte字节转换为可读的字符串,确保输出内容正确。
1万+

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



