【问题标题】:Extract Instagram Post description using Python selenium使用 Python selenium 提取 Instagram 帖子描述
【发布时间】:2021-04-22 16:11:25
【问题描述】:

早上好, 我目前正在尝试使用 Python selenium 下载 Instagram 帖子的某个字段。具体来说,我正在尝试下载图片的标题(描述)(例如,在下图中,该部分将以文本“Thanks @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

感谢您的帮助。

【问题讨论】:

  • 什么不起作用?你有例外吗?一个空列表?它返回什么?
  • 我建议使用 css 选择器而不是完整路径。它可能会更强大
  • 顺便说一句,您要分析的帖子的地址是什么?
  • 感谢您的所有回答,我不是专门分析帖子,所以一个帖子可能是:“instagram.com/p/CN-WqM_lDBG”上面的代码引发了异常。
  • CSS 会怎么样,@Nephanth?我似乎无法正确...

标签: python selenium instagram screen-scraping


【解决方案1】:

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

试试这个:

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)

【讨论】:

    【解决方案2】:

    这对我有用。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多