网站总是用过时的眉毛标记它

2024-10-02 22:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试刮取站点https://anichart.net/,以便使用这些信息根据这些信息构建一个时间表。问题是站点总是检测到过时的浏览器(显示http://outdatedbrowser.com)。你知道吗

    <div class=noscript>We\'re sorry but AniChart requires Javascript. 
    <br>Please enable Javascript or <a 
    href=http://outdatedbrowser.com>upgrade to a modern web browser</a>. 
    </div></noscript><div class="noscript modern-browser" style="display: 
    none">Sorry, AniChart requires a modern browser.<br>Please <a 
    href=http://outdatedbrowser.com>upgrade to a newer web browser</a>.</div>

我尝试了一个常规请求,也尝试了强制用户代理,如下所示。你知道吗

    import requests

    self.url = 'https://anichart.net/Winter-2019'

    headers = {'User-agent': 'Chrome/72.0.3626.109'}

    self.page = requests.get(self.url, headers=headers)

    print(self.page.content)

我知道该站点使用javascript,并且Requests模块不会引用该站点的javascript生成部分,除非我使用其他工具或可能的Selenium。我的浏览器是最新的,所以这不应该返回一个过时的浏览器结果。你知道吗

这在几天前还不错,但是看起来他们只是更新了他们的站点,所以他们可能添加了一些阻止站点上自动请求的内容。你知道吗

编辑:

硒代码如下:

    from selenium import webdriver

    url = 'https://anichart.net/Winter-2019'

    website = webdriver.Chrome()

    website.get(url)

    print(website.page_source)

    html_after_JS = website.execute_script("return document.body.innerHTML")

    print(html_after_JS)

Tags: httpsselfdivbrowsercomhttpurlnet
1条回答
网友
1楼 · 发布于 2024-10-02 22:33:52

问题不在于浏览器检测。你知道吗

requests只是呈现JavaScript(您似乎已经知道了),现在大多数网站都使用前端JavaScript库来呈现内容。还有一些网站使用Javascript检测来防止机器人抓取网页。。。你知道吗

相反,您需要使用Selenium这样的工具,它将打开一个无头的“现代”浏览器,由您选择,您可以从那里刮下页面。但是您还没有展示该代码,所以您应该问一下这个问题?你知道吗

或者,更好的是,他们有一个API-https://github.com/AniList/ApiV2-GraphQL-Docs

The AniList & AniChart websites themselves run on the Api, so everything you can do on the sites, you can do via the Api.

相关问题 更多 >