相关疑难解决方法(0)

Python请求.403禁止

我需要解析一个网站,但我得到一个错误403 Forbidden.这是一个代码:

url = 'http://worldagnetwork.com/'
result = requests.get(url)
print(result.content.decode())
Run Code Online (Sandbox Code Playgroud)

它的输出:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

请说出问题所在.

python python-requests

43
推荐指数
2
解决办法
5万
查看次数

Python 请求 - 403 禁止 - 尽管设置了“User-Agent”标头

import requests
import webbrowser
from bs4 import BeautifulSoup

url = 'https://www.gamefaqs.com'
#headers={'User-Agent': 'Mozilla/5.0'}    
headers ={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}


response = requests.get(url, headers)
Run Code Online (Sandbox Code Playgroud)

response.status_code 返回403。我可以使用firefox/chrome浏览网站,所以这似乎是一个编码错误。

我无法弄清楚我犯了什么错误。

谢谢你。

python web-scraping python-requests

7
推荐指数
1
解决办法
1万
查看次数

即使使用 User-Agent 标头,如何修复 Python 请求的“403 Forbidden”错误?

我正在向某个 URL 发送请求。我将curl命令复制到python中。因此,所有标头都包含在内,但我的请求不起作用,并且我在 HTML 输出中收到状态代码 403 和错误代码 1020。

代码是

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    # 'Accept-Encoding': 'gzip, deflate, br',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
}

response = requests.get('https://v2.gcchmc.org/book-appointment/', headers=headers)

print(response.status_code)
print(response.cookies.get_dict())
with open("test.html",'w') as f:
    f.write(response.text)
Run Code Online (Sandbox Code Playgroud)

我也收到了 cookie,但没有得到所需的响应。我知道我可以用硒做到这一点,但我想知道这背后的原因。

注意:
我已经安装了所有库并检查了版本,但它仍然无法工作并抛出 403 错误。

python http-status-code-403 python-requests

6
推荐指数
1
解决办法
2万
查看次数