Jal*_*hal 6 python get http-headers http-status-code-403 python-requests
我正在发出 GET 请求来获取 JSON,它在任何设备上的任何浏览器中都可以正常工作,但不能通过 python 请求:
url = 'https://angel.co/autocomplete/new_tags'
params = {'query': 'sci', 'tag_type': 'MarketTag'}
resp = requests.get(url,params=params)
resp.raise_for_status()
Run Code Online (Sandbox Code Playgroud)
给出HTTPError: 403 Client Error: Forbidden for url: https://angel.co/autocomplete/new_tags?query=ab&tag_type=MarketTag
所以我尝试:
Request Headersfirefox 的 JSON 响应部分中找到的所有其他标头,但仍然是 403!可能的原因是什么?还有其他我可以尝试使用的东西吗?
编辑:我在headers属性中使用的请求标头(在 firefox 中检查 JSON 的标头部分):
{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
'Host': 'angel.co',
'If-None-Match: 'W/"5857a9eac987138be074e7bdd4537df8"',
'TE': 'Trailers',
'Upgrade-Insecure-Requests': 1,
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0'}
Run Code Online (Sandbox Code Playgroud)
如果即使在将用户代理添加到标头后,get 请求也返回 403 Forbidden,则您可能需要添加更多标头,如下所示:
headers = {
'user-agent':"Mozilla/5.0 ...",
'accept': '"text/html,application...',
'referer': 'https://...',
}
r = requests.get(url, headers=headers)
Run Code Online (Sandbox Code Playgroud)
在chrome中,请求标头可以在开发者工具的网络>标头>请求标头中找到。(按 F12 进行切换。)
| 归档时间: |
|
| 查看次数: |
4568 次 |
| 最近记录: |