首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    Python使用requests库做爬虫时,乱码问题解决方案

    分析: res = requests.get(“http://www.baidu.com“) res.text返回的是Unicode型的数据。 使用res.content返回的是bytes型的数据。 也就是说,如果你想取文本,可以通过res.text。 如果想取图片,文件,则可以通过res.content。 方法1:使用res.content,得到的是bytes型,再转为str url='http://news.baidu.com' res = requests.get(url) html=res.content html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore") print(html_doc) 方法2:使用res.text url="http://news.baidu.com" res=requests.get(url) res.encoding='utf-8' print(res.text) 方法3:使用res.text+apparent_encoding url="http://news.baidu.com" res=requests.get(url) res.encoding=res.apparent_encoding print(res.text)

    01
    领券