python 爬虫自适应编码解决网页乱码问题

10,778次阅读
没有评论
import io
#
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')

#方式1
response = requests.get(self.url, headers=self.head)

bm = response.encoding
 #获取网页编码

html = response.text

html = html.encode(bm) # 用网页编码 解码

html = BeautifulSoup(html, 'lxml')

#方式2
response.encoding = response.apparent_encoding
html = BeautifulSoup(response.text,'lxml')
1
liuze
版权声明:本站原创文章,由 liuze2022-04-03发表,共计317字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...