使用Python selenium提取Instagram帖子描述

2024-10-03 06:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Description

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

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

谢谢你的帮助


Tags: 代码text文本div标题driverselenium图片
2条回答

这对我有用

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

我的ig posts的类是xil3i,但使用.text时得到一个空值 . 这段代码解决了我的问题

你只是想收集所有的标签吗

试试这个:

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)

相关问题 更多 >