TypeError:request()在使用header时遇到意外的关键字参数“header”,403 error without head

2024-09-30 05:25:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从this网站获取scarp信息,但一直获取状态代码:403, 所以尝试使用header,但是得到了TypeError:request()得到了一个意外的关键字参数“header”

代码:

import requests

head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}
url = "https://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143"
pageObj = requests.get(url, header = head)
print("Status code: " + str(pageObj.status_code)) # *for testing purpose*

错误:

^{pr2}$

header from firefox dev tool

我做错什么了?在


Tags: 代码信息url网站request状态code关键字
2条回答

参数的名称是headers,而不是header。参见docs。在

使用pageObj = requests.get(url, headers=head)

您需要在params中设置header

import requests
head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}
url = "https://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143"
PARAMS = {'header':head} 
pageObj = requests.get(url, params = PARAMS)

相关问题 更多 >

    热门问题