首页 > 解决方案 > 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)

结果 :

获取照片链接 []

标签: pythonxpathweb-scrapinglxml

解决方案


试试这个 XPath,它应该可以工作:

(//img[@class='css-9pa8cd'])[2]/@src

如果它不起作用,请尝试使用此 XPath,因为一旦您获得 html,代码就会更改。

//img[@data-aria-label-part='']/@src

不需要硒。


推荐阅读