【问题标题】:Problem in Selenium with python (instagram crawler)Selenium 中的问题与 python(instagram 爬虫)
【发布时间】:2019-04-06 18:00:09
【问题描述】:

我刚刚写了这个 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,然后转到您提供给程序的目标用户名,然后单击关注,因此它显示以下列表。这还没有完成,它应该做其他事情,但现在我停留在这一步。

我的问题是,当我点击关注时。它打开一个小窗口。这就是您可以看到以下列表的地方,我想向下滚动此列表。见下图:

see this picture

我想向下滚动以下列表,但我的代码向下滚动主页,我的意思是背面的页面。我意识到当我将鼠标光标放在以下列表上时,我可以用鼠标滚动它,所以我决定编写一个函数让我的鼠标光标在列表上然后滚动它,但它没有成功。

有人知道我应该做什么吗?

谢谢

【问题讨论】:

    标签: python selenium selenium-webdriver web-crawler


    【解决方案1】:

    以下代码适用于我:

    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)
    

    【讨论】:

    • 感谢您的回答,这有点帮助,但并没有解决我的问题。此代码在您的机器上运行?我的显示错误,它无法找到元素。这是因为元素尚未加载。例如,我有 200 个关注者,它找不到要滚动到的第 200 个 div。
    • 谢谢,我修好了。我稍微改变了你的答案,现在可以了
    【解决方案2】:

    您为什么要费心移动鼠标并单击? 您应该能够使用 requests 之类的库来抓取帐户。

    或者,已经有一些程序可以这样做,您可以从中获得灵感。

    这里有一些:

    除非特别要求您这样做,否则我认为这不是移动光标并单击每个链接的可行解决方案。

    【讨论】:

    • 因为我想获得所有关注者并将他们列在列表中。我必须向下滚动到以下列表的按钮才能加载 html 。当我打开以下列表时,我只能访问十几个关注者,而其他关注者尚未加载到页面源中。而且我不想点击所有这些,我想激活以下列表,以便我可以滚动此页面而不是主页
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多