使用BeautifulSoup python访问网站时拒绝访问[403]。

问题描述 投票:1回答:1

我要刮痧 https:/www.jdsports.it 使用BeautifulSoup,但我得到的访问拒绝。

在我的PC上,我访问网站没有任何问题,我使用Python程序的相同用户代理,但在程序上的结果是不同的,你可以看到下面的输出。

EDIT:我想我需要cookies才能访问网站。我如何获得它们并使用它们来访问网站与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 user-agent cookies python-requests

python python-requests token user-agent
1个回答
0
投票

一开始怀疑是HTTP2的问题,但也没能解决。也许你比较幸运,这里有一个HTTP2的起点。

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 作品。

© www.soinside.com 2019 - 2024. All rights reserved.