使用 Python selenium 提取 Instagram Post 描述

Extract Instagram Post description using Python selenium

早上好, 我目前正在尝试使用 Python selenium 下载 instagram post 的某个字段。具体来说,我正在尝试下载图片的标题(描述)(例如,在下面的图片中,该部分将以文本“谢谢@lolap .....”开头,一直到主题标签。

我尝试了以下代码,但它似乎不起作用(它立即抛出异常):

caption = driver.findElement(By.xpath("/html/body/div[3]/div[2]/div/article/div[2]/div[1]/ul/div/li/div/div/div[2]/span/text()"))   #get all the caption text in a String

感谢您的帮助。

您只是想收集所有主题标签吗?

试试这个:

hashtags = driver.find_elements_by_xpath("//a[@class='xil3i']")

for tag in hashtags:
    print(tag.text)

或者,如果您要查找图片说明:

desc_text = driver.find_element_by_xpath("//span[@title='Edited']").text
print(desc_text)

这对我有用。

soup = BeautifulSoup(driver.page_source, 'html.parser')
hashtags = soup.find_all('a', class_='xil3i')
for tag in hashtags:
    print(tag.text)

我的 ig 帖子 class 是 xil3i,但我在使用 .text 时得到一个空值 .这段代码解决了我的问题。