为一个帐户抓取 Instagram 关注者

Scraping Instagram followers for an account

我正在尝试使用 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 月弃用)

我遇到过与您尝试做的事情类似的问题。目前,我在我的脚本中实现的方法使用了 Selenium 的 ActionChains class,我发现它比那里的所有 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 属性(至少我在脚本中就是这样做的)。