美文网首页
request 请求乱码 Python

request 请求乱码 Python

作者: 逍遥_yjz | 来源:发表于2021-07-13 09:00 被阅读0次

使用requests请求网页时,返回的页面信息有时是乱码
解决方法和思路过程;

代码;

def get_all(url,key):
    params = {
        'keyword':key,
        'enc':'utf-8'
    }
    response = requests.get(url=url,params=params,headers=headers)
    # 打印出所请求页面返回的编码方式
    print(response.encoding)
    # response.apparent_encoding是通过内容分析出的编码方式,这里是urf-8
    print(response.apparent_encoding)
    # 转码
    content = response.text.encode(response.encoding).decode(response.apparent_encoding)
    print(content)
    with open('jd.html','w',encoding='utf-8') as f:
        f.write(content)



if __name__ == '__main__':
    key = input('输入搜索内容:')
    url = 'https://search.jd.com/Search?'
    get_all(url,key)

不加这个参数也可以,如下:

params = {
        'keyword':key,
        'enc':'utf-8'
    }

相关文章

网友评论

      本文标题:request 请求乱码 Python

      本文链接:https://www.haomeiwen.com/subject/mrujpltx.html