【问题标题】:Unable to scroll the likers page on Instagram using Selenium and Python无法使用 Selenium 和 Python 在 Instagram 上滚动点赞页面
【发布时间】:2019-12-17 05:59:33
【问题描述】:

我正在尝试构建一个网络抓取工具,让我可以抓取 Instagram 上帖子的“喜欢者”的姓名。我能够自动化该程序以打开 Instagram、登录、发布帖子并打开“被 (n) 人点赞”列表。

然后这是下面的代码,目的是抓取喜欢的人的名字。由于一次只显示最喜欢的前 11 个人,其余的只会在滚动后显示(这会触发 AJAX 请求,因为我从其他有类似问题的人那里读到),所以我编写了这段代码,它应该是第一个十一个名字,将它们附加到列表中,打印出来,然后滚动。

no_of_pagedowns = 5

while no_of_pagedowns:

    liker_list = []
    likers = driver.find_elements_by_class_name("qyrsm")    

    for n in likers:

        #scrape the name of the likers
        liker = n.find_element_by_class_name("_4EzTm").get_attribute("textContent")
        liker_list.append(liker)

    no_of_pagedowns -= 1

    print(liker_list)

    time.sleep(1)

    driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div')

    #scrollcode
    driver.execute_script("javascript:window.scrollBy(0,660)") 

它可以工作并且可以返回前 11 个“喜欢的人”的名字,但它不会滚动。我认为问题在于代码未能专注于正确的元素,但我未能找出我应该关注哪个元素。它只给出了五次相同的 11 个人。我还尝试从

替换“滚动代码”
    driver.execute_script("javascript:window.scrollBy(0,660)") 

类似

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")

    elem.send_keys(Keys.PAGE_DOWN)

where elem = driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div')

我也试过这段代码:

no_of_pagedowns = 5

while no_of_pagedowns:

    liker_list = []
    likers = driver.find_elements_by_class_name("qyrsm")    

    for n in likers:

        #scrape the name of the likers
        liker = n.find_element_by_class_name("_4EzTm").get_attribute("textContent")
        liker_list.append(liker)

    no_of_pagedowns -= 1

    print(liker_list)

    time.sleep(1)

    driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div').click()

    driver.execute_script("window.scrollBy(0, 660);")

我在找到元素后放了一个 .click() ,因为我认为可能需要单击以专注于“喜欢列表”,它实际上使滚动发生了。但是现在出现了另一个问题:无论我输入的 y cood 的值是多少,它只会在每个循环中向下滚动 1 个喜欢的人,并且在第 5 次循环时,它会失败,因为它将单击从顶部计数的第 6 个喜欢的人,领先到第 6 个喜欢的人的个人资料页面。

我已经被这个滚动问题困扰了将近一个星期。我用谷歌搜索并阅读了很多人的类似问题,但找不到有帮助的问题。我非常感谢任何可以提供帮助的人。

【问题讨论】:

标签: python selenium selenium-webdriver instagram infinite-scroll


【解决方案1】:

我不确定您到底在寻找什么,但这是我在遇到滚动问题时在 java 中尝试过的。该元素不会自动滚动到视图中,并且会滚动设置数量的对象,因此我们编写了以下代码

   public static void scrollintoView(WebDriver driver, String Value) throws 
   InterruptedException {

    int totalAttempts = 30;

    while (totalAttempts > 0) { 

        totalAttempts = totalAttempts-1;


        try { // checks code for exceptions

            WebElement ele=  driver.findElement(By.xpath(Value));

            break; // if no exceptions breaks out of loop
        } 
        catch (org.openqa.selenium.NoSuchElementException e) { 

            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("window.scrollBy(0, 300)");


          continue; // continues to loop if exception is found

        }}

    }

您在 python 中尝试类似的事情,并且硬滚动可以设置您想要在每个滚动处检查的用户数。 让我知道这是否有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多