【发布时间】:2019-03-12 23:01:24
【问题描述】:
我无法使用 requests_html 提取正确的结果:
>>> from requests_html import HTMLSession
>>> session = HTMLSession()
>>> r = session.get('https://www.amazon.com/dp/B07569DYGN')
>>> r.html.find("#productDetails_detailBullets_sections1")
[]
我可以在源内容中找到id'productDetails_detailBullets_sections1':
>>> """<table id="productDetails_detailBullets_sections1" class="a-keyvalue prodDetTable" role="presentation">""" in r.text
True
其实这个问题在 PyQuery 中也同样存在。
为什么requests_html 找不到这个元素?
【问题讨论】:
-
我可以确认源包含
<table id="productDetails_detailBullets_sections1" ...,而.find返回一个空数组。 -
r.html.find('table')也没有找到那个对象,似乎PQuery()对这里的某些事情不满意。 -
@MartijnPieters 是的,它只提取了四个表。
-
PQuery 库只是将 CSS 选择器转换为有效的
"descendant-or-self::*[@id = 'productDetails_detailBullets_sections1']XPath 表达式。所以这现在归结为 lxml;r.html.pq[0].xpath("descendant-or-self::table")确实只返回 4 个元素。 -
PyQuery(r.html.html, parser='html5')('#productDetails_detailBullets_sections1')找到元素。
标签: python python-3.x pyquery python-requests-html