Lxml xpath returns 一个空数组

Lxml xpath returns an empty array

伙计们,我正在抓取: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

不需要硒。

感谢大家的帮助。我不得不为此使用 selenium,否则请求无法正常工作,在 xpath 思想中仅选择数字 2 img 仍然有一些问题。我从数组中手动​​选择,仍然有效...

完整的工作代码

import requests
from lxml import html
from selenium import webdriver
import time

finalurl = "https://twitter.com/iForex_com/status/1019547735614255104"
browser = webdriver.Safari()
browser.get(finalurl)
time.sleep(1)

tree = html.fromstring(browser.page_source)
print("getting photolink")

postPhotoLink = tree.xpath('//img[@class="css-9pa8cd"]/@src')
print(postPhotoLink[1])

browser.close()