我刚刚写了这个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,然后转到您为程序提供的目标用户名,然后单击“ follow”,因此显示以下列表。这个还没有完成,它应该做其他的事情,但是现在我停留在这个步骤上。
我的问题是,当我单击以下内容时。它打开一个小窗口。那就是您可以在其中看到以下列表的地方,我想向下滚动此列表。看到下面的图片:
我想向下滚动以下列表,但我的代码向下滚动主页,我的意思是背面的页面。我意识到,当我将鼠标光标放在下面的列表上时,可以用鼠标滚动它,因此我决定编写一个函数来将鼠标光标放在列表上,然后滚动它,但是没有成功。 >
有人知道我该怎么办?
谢谢
答案 0 :(得分:2)
以下代码对我来说很好用:
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)
答案 1 :(得分:0)
为什么要麻烦移动鼠标并单击? 您应该可以使用requests之类的库来抓取帐户。
或者,已经有这样做的程序,您可以从中得到启发。
这里有一些:
除非明确要求您这样做,否则我认为这不是一种可行的解决方案,可以移动光标并单击每个链接。