使用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'
}
网友评论