滚动 Instagram 对话框
Scrolling Instagram Dialog Box
我正在尝试制作一个 Python 代码来做这件事:抓取用户名 X 的关注者,以及所有喜欢 X 的特定照片的用户。
问题是我正面临一个会影响这两个代码的问题。
现在我正在做第一部分,那就是刷粉丝。我已经能够获得前 12 个用户名,问题是要获得更多用户,我需要向下滚动 Instagram 关注者框,但我无法做到这一点。已经在论坛上找到了一些代码,但是 none 似乎对我有用。
这是现在所做的:
#!/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 个用户名,那就太好了。实际上,任何类型的卷轴都已经对我有很大帮助了。
感谢
您可以使用硒注入 javascript execute_script(脚本)
driver.execute_script("window.scrollBy(0, 420)")
这对我 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)
我正在尝试制作一个 Python 代码来做这件事:抓取用户名 X 的关注者,以及所有喜欢 X 的特定照片的用户。 问题是我正面临一个会影响这两个代码的问题。
现在我正在做第一部分,那就是刷粉丝。我已经能够获得前 12 个用户名,问题是要获得更多用户,我需要向下滚动 Instagram 关注者框,但我无法做到这一点。已经在论坛上找到了一些代码,但是 none 似乎对我有用。
这是现在所做的:
#!/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 个用户名,那就太好了。实际上,任何类型的卷轴都已经对我有很大帮助了。
感谢
您可以使用硒注入 javascript execute_script(脚本)
driver.execute_script("window.scrollBy(0, 420)")
这对我 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)