简体   繁体   中英

How to avoid requests.exceptions.ConnectionError for printing url text?

I cannot print the text of the url. What am I doing wrong?

import requests

r = requests.get('https://www.autozone.com/ecomm/b2c/v1/browse/page/getProductFitVehicles?skuId=937722&productLineCode=NGK&partNumber=4469')

print(r.text)

Traceback

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

You need to set a user agent, it doesnt even have to be something coherent

import requests
#Set headers
headers = {'user-agent': 'my-app/0.0.1'}
#Add , headers=headers at the end of line before closing the parentheses
r = requests.get('https://www.autozone.com/ecomm/b2c/v1/browse/page/getProductFitVehicles?skuId=937722&productLineCode=NGK&partNumber=4469', headers=headers)

print(r.text)

r is not a variable but an object. Seems like you want to print the responce, try this:

print(r.content)

OR

print(r.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM