如何避免request.exceptions.ConnectionError打印URL文本?

问题描述

我无法打印网址文本。我在做什么错了?

import requests

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

print(r.text)

追踪

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

解决方法

您需要设置一个用户代理,它甚至不必是连贯的

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不是变量,而是对象。好像您要打印响应,请尝试以下操作:

print(r.content)

OR

print(r.text)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...