【问题标题】:How to get the number of post a user has in instagram?如何获取用户在 Instagram 中的帖子数量?
【发布时间】:2018-12-31 15:19:44
【问题描述】:

我正在使用 selenium python 创建一个 instagram 机器人。我只是想知道如何检索用户在其个人资料页面中包含的帖子的编号。这样我就可以在我的脚本中处理“NO POSTS YET”条件。

谁能给我一个提示。我不需要完整的代码,也不需要提示我到正确的地方。

示例配置文件:https://www.instagram.com/michelemessina/

【问题讨论】:

  • 使用 Instagram API,而不是抓取个人资料。

标签: python selenium selenium-webdriver instagram bots


【解决方案1】:

可以通过xPath获取元素的文本:

//span[text() = ' posts']/span

【讨论】:

  • 谢谢,但我会选择 window._sharedData.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media
【解决方案2】:

我更喜欢使用 find_element_by_xpath,因为 xpath 在 chrome 中很容易找到。

方法如下:右键单击 -> 检查 -> 右键单击​​ -> 复制 -> CopyXpath

xpath = '//*[@id="react-root"]/section/main/div/header/section/ul/li[1]/span/span' if browser.find_element_by_xpath(xpath).text == '0': pass

【讨论】:

    【解决方案3】:

    根据您的任务,我写了几行来获取帖子、关注者和关注的数量。您可以运行代码查看结果。

    browser = webdriver.Chrome()
    browser.get('https://www.instagram.com/michelemessina/')
    wait = WebDriverWait(browser, 5)
    
    posts = wait.until(EC.presence_of_element_located((By.XPATH, '//li/span[text()=" posts"]/span'))).text
    followers = wait.until(EC.presence_of_element_located((By.XPATH, '//li/a[text()=" followers"]/span'))).text
    following = wait.until(EC.presence_of_element_located((By.XPATH, '//li/a[text()=" following"]/span'))).text
    print('Posts: {}; Followers: {}; Following: {}.'.format(posts, followers, following))
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多