Py学习  »  Python

python AsyncHTMLSession:您没有访问此服务器上“XXX”的权限

SamP • 3 年前 • 1289 次点击  

我想使用python访问一个网站 AsyncHTMLSession 从…起 requests_html 图书馆
这是我的代码:

from requests_html import AsyncHTMLSession
import asyncio

async def connect_to_site(url):
    session = AsyncHTMLSession()
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"}
    res = await session.get(url, headers=headers)
    print(res)
    await res.html.arender(sleep=5, timeout=30)
    print(res.html.full_text)

url = 'https://www.otcmarkets.com'

asyncio.run(connect_to_site(url))

执行代码后,我得到以下打印:

<回应[200]>
拒绝访问
拒绝访问
你没有权限访问“http://www.otcmarkets.com/“在这个服务器上。
参考#18.9c4519d4。1643149046.338b64e3

可能是什么问题?我怎样才能克服它?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130744
文章 [ 1 ]  |  最新文章 3 年前
Alex Kosh
Reply   •   1 楼
Alex Kosh    3 年前

我认为这是某种机器人检测。然而 requests_html 可以呈现JS,它不是真正的浏览器,不能完全绕过机器人保护。

你可以使用一些库来控制真正的浏览器,比如 playwright / selenium / puppeteer

下面是一个例子 剧作家 :

from playwright.sync_api import sync_playwright

URL = 'https://www.otcmarkets.com'

with sync_playwright() as p:
    # Webkit is fastest to start and hardest to detect
    browser = p.webkit.launch(headless=True)

    page = browser.new_page()
    page.goto(URL)

    html = page.content()

print(html)