python 爬虫 HTML 乱码

请问各位这种html乱码是需要解压缩吗,还是其他原因导致的!
import requests
url=’https://movie.douban.com/top250'
header={ ‘Accept’:’text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,imag e/apng,/;q=0.8’,
‘Accept-Encoding’:’gzip,deflate,br’,
‘Accept-Language’:’zh-CN,zh;q=0.9’,
‘Cache-Control’:’max-age=0’,
‘Connection’:’keep-alive’,
‘Host’:’movie.douban.com’,
‘Upgrade-Insecure-Requests’:’1’,
‘User-Agent’:’Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/63.0.3239.132 Safari/537.36’,
}
response=requests.get(url=url,headers=header)
response.encoding=’utf-8’
print(response.text)
结果

Jason990420
最佳答案
  1. headr'Accept-Encoding':'gzip,deflate,br'
  2. response.headers'Content-Encoding': 'br'
    {'Date': 'Mon, 09 Mar 2020 12:24:34 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection' : 'keep-alive', 'Keep-Alive': 'timeout=30', 'Vary': 'Accept-Encoding, Accept-Encoding', 'X-Xss-Protection': '1; mode=block', ' X-Douban-Mobileapp': '0', 'Expires': 'Sun, 1 Jan 2006 01:00:00 GMT', 'Pragma': 'no-cache', 'Cache-Control': 'must-revalidate, no-cache, private', 'Set-Cookie': 'bid=yFVQdCa6UaE; Expires=Tue, 09-Mar-21 12:24:34 GMT; Domain=.douban.com; Path=/', 'X-DOUBAN -NEWBID': 'yFVQdCa6UaE', 'X-DAE-App': 'movie', 'X-DAE-Instance': 'default', 'Server': 'dae', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'br'}
  3. br 指的是 Brotli,是一种全新的数据格式,无损压缩,压缩比极高 (比gzip高的) , 默认是不作br解压缩的.
  4. 解决方法有两种方式
    'Accept-Encoding':'gzip,deflate,br'
    去掉br, 改为 
    'Accept-Encoding':'gzip,deflate'
    pip install brotli
    import brotli
    data = brotli.decompress(response.content)
    html = data.decode('utf-8')
4年前 评论
jean123 (楼主) 4年前
讨论数量: 3
pardon110

你所抓取的目标页面编码有可能是gb2312

4年前 评论
jean123 (楼主) 4年前

代码请复制粘贴,换一下吧。图片贴代码 太伤害人了。哈哈:)

4年前 评论
jean123 (楼主) 4年前
Jason990420
  1. headr'Accept-Encoding':'gzip,deflate,br'
  2. response.headers'Content-Encoding': 'br'
    {'Date': 'Mon, 09 Mar 2020 12:24:34 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection' : 'keep-alive', 'Keep-Alive': 'timeout=30', 'Vary': 'Accept-Encoding, Accept-Encoding', 'X-Xss-Protection': '1; mode=block', ' X-Douban-Mobileapp': '0', 'Expires': 'Sun, 1 Jan 2006 01:00:00 GMT', 'Pragma': 'no-cache', 'Cache-Control': 'must-revalidate, no-cache, private', 'Set-Cookie': 'bid=yFVQdCa6UaE; Expires=Tue, 09-Mar-21 12:24:34 GMT; Domain=.douban.com; Path=/', 'X-DOUBAN -NEWBID': 'yFVQdCa6UaE', 'X-DAE-App': 'movie', 'X-DAE-Instance': 'default', 'Server': 'dae', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'br'}
  3. br 指的是 Brotli,是一种全新的数据格式,无损压缩,压缩比极高 (比gzip高的) , 默认是不作br解压缩的.
  4. 解决方法有两种方式
    'Accept-Encoding':'gzip,deflate,br'
    去掉br, 改为 
    'Accept-Encoding':'gzip,deflate'
    pip install brotli
    import brotli
    data = brotli.decompress(response.content)
    html = data.decode('utf-8')
4年前 评论
jean123 (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!