【问题标题】:Scrolling Instagram Dialog Box滚动 Instagram 对话框
【发布时间】:2019-07-21 18:05:24
【问题描述】:

我正在尝试编写一个 Python 代码来做这件事:刮掉用户名 X 的追随者,以及所有喜欢 X 的特定照片的用户。 问题是我面临一个会影响这两个代码的问题。

现在我正在处理第一部分,即抓取追随者。我已经能够获得前 12 个用户名,问题是要获得更多用户,我需要向下滚动 Instagram 关注者框,但我无法做到这一点。已经在论坛上看到了一些代码,但它们似乎都不适合我。

这就是现在所做的:

#!/usr/bin/env python
# -- coding: utf-8 --

import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('C:/Users/User/Desktop/chromedriver')

#login
username = ""  ##user
password = ""  ##pass
driver.get("https://www.instagram.com/accounts/login/")
driver.find_element_by_xpath("//input[@name='username']").send_keys(username)
driver.find_element_by_xpath("//input[@name='password']").send_keys(password)
driver.find_element_by_xpath("//input[@name='password']").submit()
time.sleep(4)

account = "vans"

link = 'https://www.instagram.com/' + account

driver.get(link)

driver.find_element_by_partial_link_text("follower").click()
time.sleep(2)


html = driver.page_source

soup = BeautifulSoup(html, "html.parser")

followers = soup.find_all("a", class_='FPmhX notranslate _0imsa')

for user in followers:
    print(user.text)

所以,如果有人可以帮助我制作 these 对话框向下滚动到第 12 个用户名,这会很棒。实际上,任何类型的卷轴都对我有很大帮助。

谢谢

【问题讨论】:

  • FWIW:抓取 instagram 违反了他们的服务条款,并且(可能)会阻止你。

标签: python selenium-webdriver web-scraping instagram


【解决方案1】:

您可以使用 seleniums execute_script(script) 注入 javascript

driver.execute_script("window.scrollBy(0, 420)")

【讨论】:

  • 还请注意,instagrams 网站是通过 react.js 动态生成的,因此您访问的那些类名可能会更改,以试图停止抓取
【解决方案2】:

这在 2019 年对我有用...

FList = driver.find_element_by_css_selector('div[role=\'dialog\'] ul')
numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))

FList.click()
actionChain = webdriver.ActionChains(driver)
time.sleep(random.randint(2,4))

while (numberOfFollowersInList < max):
    actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()        
    numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
    time.sleep(0.4)
    print(numberOfFollowersInList)
    actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()            
    time.sleep(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 2020-04-24
    相关资源
    最近更新 更多