python - lxml 将返回空的 lisy 但 requests_html 将返回想要的结果
问题描述
我写了两个代码,一个带有请求,另一个带有 request_html 的 lxml。带有 requests 和 lxml 的代码将返回一个空列表,而带有 request_thml 的代码将返回想要的结果。
代码1:
from lxml import html
url = 'https://www.amazon.com/OnePlus-Glacial-Unlocked-Android-Smartphone/dp/B08723759H/ref=sr_1_1?dchild=1&keywords=oneplus&qid=1624692877&sr=8-1'
page = requests.get(url)
content = html.fromstring(page.text)
price = content.xpath('//*[@id="productTitle"]')
print(price)
#output is always
#[]
代码 2:
from requests_html import HTMLSession
url = 'https://www.amazon.com/OnePlus-Glacial-Unlocked-Android-Smartphone/dp/B08723759H/ref=sr_1_1?dchild=1&keywords=oneplus&qid=1624692877&sr=8-1'
session = HTMLSession()
r = session.get(url)
r.html.render(sleep=1)
title = r.html.xpath('//*[@id="productTitle"]', first=True)
print(title)
#output
#OnePlus 8 Glacial Green, 5G Unlocked Android Smartphone U.S Version, 8GB RAM+128GB Storage, 90Hz Fluid Display,Triple Camera, with Alexa Built-in,
解决方案
推荐阅读
- business-intelligence - Splunk 上的 Tableau.. 可能吗?
- php - 将具有特定路径名的所有 URL 重定向到某个视图 - LARAVEL
- drupal - 在安装了嵌入和媒体库的 Drupal 8.6.17 中不显示嵌入式实体
- php - 如何使正则表达式中需要的字符?
- dsc - 如何使文件与所需的状态配置同步
- python - 如何将 Pandas 中的函数应用于同一行中的不同单元格满足条件的每一行中的单元格?
- ios - iOS 13 中未调用应用程序委托方法
- python - Urllib2 突然不工作了。请求时间
- laravel - 如何将变量从控制器传递到视图而不刷新 Laravel 中的网页?
- javascript - 为什么 eval() 不适用于导入的函数?