我刚刚写了这个instagram爬虫,这是一个大学的小项目。我会告诉你代码并上传一张图片,告诉你我的问题是什么。
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
class App:
def __init__(self,username="Enter your username here",password="Enter your password here",target_username="shriar.ha"):
self.username = username
self.password = password
self.target_username = target_username
self.driver = webdriver.Chrome("/Users/Shahriar/Desktop/Selenium and BS projects/chromedriver.exe") #This is the path to webdriver in my PC ,you should change it and give the path of where your webdriver is located.
self.main_url = "https://www.instagram.com"
self.driver.get(self.main_url)
sleep(5)
self.log_in()
self.close_notification()
self.go_to_target_profile()
sleep(3)
self.click_on_following()
self.move_mouse()
self.scroll_down()
self.driver.close()
def move_mouse(self):
actions = ActionChains(self.driver)
following_list = self.driver.find_element_by_xpath("//div[@class='isgrP']//div[@role = 'button']")
actions.move_to_element(following_list).perform()
sleep(3)
def scroll_down(self):
number_of_following = self.driver.find_element_by_xpath("//a[@href='/shriar.ha/following/']/span").get_attribute("innerHTML")
print(number_of_following)
number_of_following = int(number_of_following)
if number_of_following > 7:
number_of_scrolls = (number_of_following / 7)+3
for i in range(int(number_of_scrolls)):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(2)
def click_on_following(self):
following_button = self.driver.find_element_by_xpath("//a[@href='/shriar.ha/following/']")
following_button.click()
sleep(5)
def close_notification(self):
try:
sleep(3)
close_noti_btn = self.driver.find_element_by_xpath("//button[contains(text(),'Not Now')]")
close_noti_btn.click()
sleep(2)
except:
pass
def go_to_target_profile(self):
target_profile_url = self.main_url + "/" + self.target_username + "/"
self.driver.get(target_profile_url)
def log_in(self):
login_button = self.driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
login_button.click()
sleep(5)
username_input = self.driver.find_element_by_xpath("//input[@name='username']")
username_input.send_keys(self.username)
password_input = self.driver.find_element_by_xpath("//input[@name='password']")
password_input.send_keys(self.password)
password_input.submit()
if __name__ == "__main__":
app = App()
正如您所看到的,它在instagram中登录,然后转到您为程序提供的目标用户名,然后点击以下内容,因此它显示以下列表。这还没有完成,它应该做其他的事情,但现在我陷入了这一步。
我的问题是,当我点击以下。它打开一个小窗口。在那里你可以看到以下列表,我想向下滚动这个列表。见下图:
我想向下滚动下面的列表,但我的代码向下滚动主页面,我的意思是背面的页面。我意识到,当我将鼠标光标放在下面的列表上时,我可以用鼠标滚动它,所以我决定编写一个函数将我的鼠标光标放在列表上然后滚动它,但它没有成功。
谁知道我应该怎么做?
谢谢
以下代码适用于我:
def scroll_down(self):
number_of_following = self.driver.find_element_by_xpath("//a[@href='/shriar.ha/following/']/span").get_attribute("innerHTML")
print(number_of_following)
number_of_following = int(number_of_following)
if number_of_following > 7:
number_of_scrolls = (number_of_following / 7)+3
for i in range(int(number_of_scrolls)):
#scroll by element
self.driver.execute_script("arguments[0].scrollIntoView(true)",self.driver.find_element_by_xpath("(//div[@role='dialog']//button[text()='Follow'])["+number_of_following+"]"))
time.sleep(2)
你为什么要打扰移动鼠标并点击?您应该能够使用像requests这样的库来抓取帐户。
或者,有些程序已经在做,你可以从中受到启发。
这里有一些:
除非你被特别要求,否则我认为这不是一个可行的解决方案,可以移动光标并单击每个链接。