python - 使用 BeautifulSoup python 访问站点时访问被拒绝 [403]
问题描述
我想使用 BeautifulSoup抓取https://www.jdsports.it/ ,但访问被拒绝。
在我的电脑上访问该站点没有任何问题,并且我使用的是 Python 程序的相同用户代理,但在程序上结果不同,您可以看到下面的输出。
编辑:我认为我需要 cookie 才能访问该网站。我怎样才能得到它们并使用它们访问带有 python 程序的站点来抓取它?
- 如果我使用“ https://www.jdsports.com ”,则该脚本有效,该站点是同一站点但具有不同的区域。
谢谢!
import time
import requests
from bs4 import BeautifulSoup
import smtplib
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
url = 'https://www.jdsports.it/'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
soup.encode('utf-8')
status = soup.findAll.get_text()
print (status)
输出是:
<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>
You don't have permission to access "http://www.jdsports.it/" on this server.<p>
Reference #18.35657b5c.1589627513.36921df8
</p></body>
</html>
>
python beautifulsoup 用户代理 cookie python-requests
解决方案
起初怀疑是 HTTP/2,但也无法使其正常工作。也许你更幸运,这里是 HTTP/2 的起点:
import asyncio
import httpx
import logging
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
}
url = 'https://www.jdsports.it/'
async def f():
client = httpx.AsyncClient(http2=True)
r = await client.get(url, allow_redirects=True, headers=headers)
print(r.text)
asyncio.run(f())
(在 Windows 和 Linux 上都进行了测试。)这可能与 TLS1.2 有关吗?这就是我接下来要看的地方,就像curl
作品一样。
推荐阅读
- discord.js - 来自用户状态 discord.js v13 的问题
- java - 我可以创建一个包含 JPA 实体、存储库和可以子类化的服务的 spring-boot 库吗?
- typo3 - TYPO3:子页面菜单:按类别过滤?
- mongoose - 更新猫鼬中的嵌套子文档
- php - 将子产品的最低特价设置为其可配置产品 Magento
- python - 我正在尝试向 OVH 的 API 发送请求,但是在尝试执行此操作时出现错误
- camera - AAOS 无法在自定义模拟器上启动相机
- python - 如何在 Discord Embed 中对人员/角色进行 ping 操作?
- php - 如何根据 PHP 中的数据库表值更改按钮颜色?
- pytorch - 令牌索引序列长度大于此模型指定的最大序列长度 (28627 > 512)