python - Lxml xpath 返回一个空数组
问题描述
好吧,伙计们,我在刮:https ://twitter.com/iForex_com/status/1019547735614255104
如果有人可以帮助我,我将不胜感激
import requests
from lxml import html
finalurl = f"https://www.twitter.com/user/status/{id}"
response = requests.get(finalurl,allow_redirects=True)
tree = html.fromstring(response.content)
print("getting photolink")
postPhotoLink = tree.xpath('//*[@id="react-root"]/div/div/div/main/div/div/div/div[1]/div/div[2]/div/section/div/div/div/div[1]/div/article/div/div[4]/div/div/div/a/div/div[2]/div/img/@src')
print(postPhotoLink)
结果 :
获取照片链接 []
解决方案
试试这个 XPath,它应该可以工作:
(//img[@class='css-9pa8cd'])[2]/@src
如果它不起作用,请尝试使用此 XPath,因为一旦您获得 html,代码就会更改。
//img[@data-aria-label-part='']/@src
不需要硒。
推荐阅读
- python - 给定两个类似数组的操作数(而不是尝试广播它们),如何使 ufunc 输出矩阵?
- android - 从 Android.mk/Android.bp 打印 AOSP 中特定模块的变量
- vue.js - 基本的 Vue 商店,在整个应用程序中具有相关的 API 调用
- amazon-web-services - 需要 AWS 存储架构帮助
- c - 告诉 C 内联函数,但仍然可以调用调试器
- nativescript - mediafilepicker:如何访问选定的文件
- winforms - 隐藏控件而不留空白
- node.js - 从命令行运行 `node index.js` 有效,但是当我在 package.json 中使用启动脚本时,我收到 EADDRINUSE /usr/bin/local/node 错误
- jenkins - Jenkins 管道作业 - 未创建工作区目录
- python - 如何在从代码中获得的 csv 文件中打印网页抓取结果