python - TypeError : request() got an unexpected keyword argument 'header' - when i use header, 403 错误 - 没有 header

标签 python html python-requests http-status-code-403

我正在尝试从 this抓取 信息网站,但不断收到状态代码:403, 所以尝试使用 header 但得到 TypeError: request() got an unexpected keyword argument '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*

错误:

Traceback (most recent call last):
  File "F:/Python/PyCharm Community Edition 2019.2.3/Workshop/WEB_SCRAPING/test2.py", line 6, in <module>
    pageObj = requests.get(url, header = head)
  File "F:\Python\PyCharm Community Edition 2019.2.3\Workshop\WEB_SCRAPING\venv\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "F:\Python\PyCharm Community Edition 2019.2.3\Workshop\WEB_SCRAPING\venv\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
TypeError: request() got an unexpected keyword argument 'header'

header from firefox dev tool

我做错了什么?

最佳答案

参数的名称是headers,而不是header。查看docs .

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

关于python - TypeError : request() got an unexpected keyword argument 'header' - when i use header, 403 错误 - 没有 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491690/

相关文章:

python - 在 if 语句中实现 for 循环

Python HTTP 请求 URL 结束斜杠

html - Facebook 喜欢使用 edge.create 的回调事件不起作用

javascript - 使用选定值通过 angularjs 过滤数据

Python3 :requests module headers

python - 回调查询 Telegram 游戏

python - 掷 6 面骰子到距起点 N 格有多少种可能?

python - 有没有办法退出 pytest 测试并继续下一个测试?

python - Keras:从保存的模型中获取训练数据

javascript - 清空 IFrame 上的内容的最佳方法是什么