Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

Requests Different Data Between Python 2 vs Python 3

I am having an issue converting some simple get request code from Python 2 to 3. Running the same code with same version of requests (2.25.1) I get different response headers as well as content. Using Py2, I get content/text properly. However the with same code in Py3 I get garbled content/text. Is there a difference in how Py3 handles a get request??

response = requests.get(url)
print (response.headers)
print (response.text)

Header response, Py2 vs 3. Note some proprietary data was removed from the Py2 header. Data does not appear in Py3 header.

{'X-Powered-By': 'ASP.NET', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Connection': 'close', 'Date': 'Tue, 26 Jan 2021 16:36:11 GMT', 'Server': 'Microsoft-IIS/6.0', 'Content-Type': 'text/plain; charset=utf-8' }

{'Connection': 'close', 'Date': 'Tue, 26 Jan 2021 16:36:16 GMT', 'Content-Type': 'text/plain; charset=utf-8', 'Server': 'Microsoft-IIS/6.0'}

Example py3 content.

a
▼     ?
408
?Ao?8?????V.??BI???B?.????????V??♀??N???Z???N)??n??]?U2L?~?↓????♀?aX??%p?2?????????;?(?`5???k,HFS9???????n??

I have tried to .encode('utf-8') the response.text but I get a string like a x1fxefxbfxbdx08x00x00 Why is there a difference between the same request in different versions of python?

question from:https://stackoverflow.com/questions/65905655/requests-different-data-between-python-2-vs-python-3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about to try:

print(response.encoding)
print(response.content)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...