Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取请求-响应文本打印无效字符_Python_Python Requests - Fatal编程技术网

Python 获取请求-响应文本打印无效字符

Python 获取请求-响应文本打印无效字符,python,python-requests,Python,Python Requests,我不是一个有经验的程序员,所以我提前道歉 我经常使用BeautifulSoup等工具进行简单的网页抓取,然后继续我的工作。最近,在一些网站上,我一直有一个问题,我似乎无法搜索或了解自己 r = requests.get('https://www.sneakersnstuff.com/', headers=headers) print(r.text) 当它打印出来的时候,和往常不同,它看起来很像。 提前谢谢 编辑: r、 内容也不起作用。只是一堆“\x83\xff\x7f\x8c

我不是一个有经验的程序员,所以我提前道歉

我经常使用BeautifulSoup等工具进行简单的网页抓取,然后继续我的工作。最近,在一些网站上,我一直有一个问题,我似乎无法搜索或了解自己

    r = requests.get('https://www.sneakersnstuff.com/', headers=headers)
    print(r.text)
当它打印出来的时候,和往常不同,它看起来很像。 提前谢谢

编辑: r、 内容也不起作用。只是一堆“\x83\xff\x7f\x8cH\xcd\xea\”等

标题:

        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7',
'cache-control': 'max-age=0',
'referer': 'https://www.sneakersnstuff.com/en/858/new-arrivals',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'

你应该多读一些关于

这将暂时解决您的问题,但这不是正确的方法。一旦您阅读了更多关于Unicode的内容,您就会明白为什么下面的解决方案不能始终有效

r = requests.get('https://www.sneakersnstuff.com/', headers=headers)
print(r.text.encode('ascii', 'ignore').decode('ascii'))

你应该多读一些关于

这将暂时解决您的问题,但这不是正确的方法。一旦您阅读了更多关于Unicode的内容,您就会明白为什么下面的解决方案不能始终有效

r = requests.get('https://www.sneakersnstuff.com/', headers=headers)
print(r.text.encode('ascii', 'ignore').decode('ascii'))
发件人:

以unicode表示的响应内容

如果
Response.encoding
为None,将使用
chardet
猜测编码

响应内容的编码仅基于 HTTP头,完全遵循RFC2616。如果你能 利用非HTTP知识更好地猜测 编码,您应该在访问之前适当地设置r.encoding 这是我的财产

换句话说,
Response.text
对网页内容的编码做出了错误的猜测,因为标题中缺少此类信息

您需要使用以下内容指定内容的编码:

r.encoding = 'utf-16' # or whatever the encoding of the content really is
在访问
r.text

之前,请从以下位置访问:

以unicode表示的响应内容

如果
Response.encoding
为None,将使用
chardet
猜测编码

响应内容的编码仅基于 HTTP头,完全遵循RFC2616。如果你能 利用非HTTP知识更好地猜测 编码,您应该在访问之前适当地设置r.encoding 这是我的财产

换句话说,
Response.text
对网页内容的编码做出了错误的猜测,因为标题中缺少此类信息

您需要使用以下内容指定内容的编码:

r.encoding = 'utf-16' # or whatever the encoding of the content really is

在访问
r.text
之前,请删除
'accept-encoding'
标题。您看到的似乎是压缩内容。

删除
'accept-encoding'
标题。你看到的似乎是压缩的东西。

如果你尝试
print(r.content)
,会发生什么?另外,您可以包括您正在使用的标题吗?请包括直接从chrome网络选项卡复制的标题。'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,图像/webp,图像/apng,/;q=0.8’,‘接受编码’:‘gzip,deflate,br’,‘接受语言’:‘en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7','cache-control':'max age=0','referer':'','upgrade-unsecure-requests':'1','user-agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/67.0.3396.99 Safari/537.36'如果您尝试
print(r.content)
,会发生什么?另外,您可以包括您正在使用的标题吗?请包括直接从chrome网络选项卡复制的标题。'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,图像/webp,图像/apng,/;q=0.8’,‘接受编码’:‘gzip,deflate,br’,‘接受语言’:‘en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7','cache-control':'max age=0','referer':'','upgrade-unsecure-requests':'1','user-agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/67.0.3396.99 Safari/537.36'去掉你的头,你会看到
HTML
文件我看到你在提出这个问题后编辑了头文件-我完全错过了。去掉你的头文件,你会看到
HTML
文件我看到你在提出这个问题后编辑了头文件-我完全错过了。你回答这个问题就这样做了,效果很好!非常感谢你!按照你的回答去做,结果成功了!非常感谢你!