【问题标题】:Scraping Instagram followers for an account为帐户抓取 Instagram 关注者
【发布时间】:2021-01-23 16:53:39
【问题描述】:

我正在尝试使用 Selenium 从 Instagram 帐户中获取关注者。 (https://www.instagram.com/france/followers/)

我需要向下滚动一个弹出页面,但我唯一可以向下滚动的是后页。

这是我的代码

scr1 = driver.find_element_by_xpath('/html/body/div[4]/div/div')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);",scr1)  ## Scroll to bottom of page with using driver

我还尝试在我的 JS 浏览器控制台中使用我的对话框模式的 JSPath:

window.scrollTo(0, document.querySelector("body > div.RnEpo.Yx5HN > div"));

(已经看到与 Stack 相关的帖子,但答案似乎在 2020 年 10 月已弃用)

【问题讨论】:

    标签: python selenium web-scraping instagram


    【解决方案1】:

    我在您尝试做的事情上遇到了类似的问题。目前,我在脚本中实现的方法使用 Selenium 的 ActionChains 类,我发现它比所有 Javascript (execute_script) 答案都更有帮助。

    基本上我使用ActionChains 按下“向下”箭头键,然后操纵它对我有利。我将在下面列出基本语法:

    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.action_chains import ActionChains
    actions = ActionChains(browser)
    actions.send_keys(Keys.DOWN).perform() #sends a general down arrow key to the page 
    

    现在我将ActionChains 聚焦在一个特定元素上,在本例中是弹出窗口。有很多方法可以专注于元素,但我发现最简单的解决方法是在使用 ActionChains

    之前单击元素

    像这样:

    popup = driver.find_element_by_class_name('popupClass') #you don't have to use class_name
    for i in range(500): #500 is amount of scrolls
        popup.click() #focus the down key on the popup
        actions.send_keys(Keys.DOWN).perform() # press the down key
    

    上述方法将单击弹出窗口,然后按一次向下键,本质上是向弹出窗口而不是整个页面发送 500 个“滚动”。完成后,您会想要获取负责弹出窗口的元素上的 .text 属性(至少我在脚本中是这样做的)。

    【讨论】:

    • 您好@ohan-shah ty 为您解答。您的代码中的“浏览器”是什么?它在这一行给我一个错误,无法弄清楚
    • 我的名为“browser”的变量与名为“driver”的变量相同。如果您复制我的代码,只需将单词browser 替换为driver。另外,请确保使用实际的类名填写popup 类名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多