首页 > 解决方案 > requests-html 未能获得预期的元素

问题描述

我是 requests-html 的新手,几天前刚刚安装。

遵循教程时:

from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://python.org/')
about = r.html.find('#about', first=True)
print(about.text)

教程描述的预期输出是:

About
Applications
Quotes
Getting Started
Help
Python Brochure

但实际上我得到了以下信息:

About
Applications
Quotes
Getting Started
Help
Python Brochure
Downloads
All releases
Source code
Windows
macOS
Other Platforms
License
Alternative Implementations
Documentation
.
.
.
Submit Website Bug
Status
Copyright ©2001-2021. Python Software Foundation Legal Statements Privacy Policy Powered by Heroku
window.jQuery || document.write('<script src="/static/js/libs/jquery-1.8.2.min.js"></script>') window.jQuery || document.write('<script src="/static/js/libs/jquery-ui-1.12.1.min.js"></script>')

这是从元素 <li id="about" ... 到整个 html 文档的末尾。

有人知道这个问题吗?

更新:

感谢@DarkKnight 和@CrazyChucky,

但实际上请求的 HTML 元素还没有改变:

<li id="about" class="tier-1 element-1  " aria-haspopup="true">
    <a href="/about/" title="" class="">About</a>
    <ul class="subnav menu" role="menu" aria-hidden="true">
        <li class="tier-2 element-1" role="treeitem"><a href="/about/apps/" title="">Applications</a></li>
        <li class="tier-2 element-2" role="treeitem"><a href="/about/quotes/" title="">Quotes</a></li>
        <li class="tier-2 element-3" role="treeitem"><a href="/about/gettingstarted/" title="">Getting Started</a></li>
        <li class="tier-2 element-4" role="treeitem"><a href="/about/help/" title="">Help</a></li>
        <li class="tier-2 element-5" role="treeitem"><a href="http://brochure.getpython.info/" title="">Python Brochure</a></li>
    </ul>
</li>

标签: pythonelementpython-requests-html

解决方案


推荐阅读