Python 具有自定义标头的请求不返回AttributeError
运行此代码将返回AttributeException: 回溯(最近一次呼叫最后一次): 文件“c:\Users\ahnlu\OneDrive\desktop\code\my\u project\test2.py”,第24行, 在里面 r=requests.get(url,headers=header)Python 具有自定义标头的请求不返回AttributeError,python,Python,运行此代码将返回AttributeException: 回溯(最近一次呼叫最后一次): 文件“c:\Users\ahnlu\OneDrive\desktop\code\my\u project\test2.py”,第24行, 在里面 r=requests.get(url,headers=header) 如果你查阅文档,你会发现标题应该是一个字典,它又有一个dict.items()属性: 如果您想向请求添加HTTP头,只需传入一个dict 添加到headers参数 例如,在上一个示例中,我们没有
如果你查阅文档,你会发现
标题
应该是一个字典,它又有一个dict.items()
属性:
如果您想向请求添加HTTP头,只需传入一个dict
添加到headers参数
例如,在上一个示例中,我们没有指定我们的用户代理:
AttributeError: 'set' object has no attribute 'items'
您使用的是一个集合
-它没有set.items()
属性
修复:使用
header={“用户代理”:“我的用户代理”}
OMG。。我的错误。我写了‘用户代理:我的应用程序/0.0.1’->‘用户代理’:‘我的应用程序/0.0.1’没问题。谢谢
AttributeError: 'set' object has no attribute 'items'
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)