【问题标题】:Scroll down list instagram selenium and python向下滚动列表 instagram selenium 和 python
【发布时间】:2017-11-09 21:33:46
【问题描述】:

我为 instagram 写了一个脚本。我需要一个方法来返回我的关注者列表。我的追随者不可见(只有 10 个),我必须向下滚动页面。我正在使用 selenium webdriver 和 python 来自动化这个过程。但不幸的是它没有向下滚动。这是我的代码

    def get_followers(self):
    try:
        driver.find_elements_by_css_selector('a._t98z6')[0].click()
    except Exception as e:
        print("Sorry, i don't have access to your followers: {0}".format(e))
    else:
    followers = []
                while True:
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
            try:
                WebDriverWait(driver, 20).until(lambda x: x.find_element_by_css_selector("li._6e4x5"))
            except:
                break
        followers = driver.find_elements_by_css_selector("a._2g7d5.notranslate._o5iw8")

     return followers

任何解决方案将不胜感激。谢谢。

【问题讨论】:

    标签: python list selenium scroll instagram


    【解决方案1】:
    followers_panel = browser.find_element_by_xpath(XPATH)
    i = 1
    while i < number_of_followers:
        try:
            follower = browser.find_element_by...
            i += 1
        except NoSuchElementException:
            self.browser.execute_script(
                "arguments[0].scrollTop = arguments[0].scrollHeight",followers_panel
            )
    

    这样你就可以抓取所有的关注者了。

    【讨论】:

      【解决方案2】:

      我设法使用 ActionChain 滚动滚动。但是随着列表的增长和你电脑+互联网的速度,它变得很慢。起初只有 20-24 个名字,每个卷轴你可以得到 n 10 个名字。然后我习惯在单击最后一个元素时单击最后一个元素,再次单击最后一个元素时会出现 10 个新用户。就这样吧。

      from selenium.webdriver.common.action_chains import ActionChains
      
      def get_list():
          element=[]
          ran_num=int(random.randint(0,len(subject)-1))
          search(subject[ran_num])
          br.find_element_by_class_name("_e3il2").click() #open first image
          time.sleep(2)
          br.find_element_by_partial_link_text('likes').click()
          time.sleep(2)
      
          while len(element)<150:
                  element=br.find_elements_by_xpath("//*[@class='_9mmn5']")
                  i=len(element)-1
                  element[i].click()
                  time.sleep(1.50)
      
          likers=br.find_elements_by_xpath("//*[@class='_2g7d5 notranslate _o5iw8']") #get the username
          for i in range(len(likers)):
                  insta_id=likers[i].text
                  if (insta_id not in main_list):
                      main_list.append(insta_id)
                      with open ('to_like.txt','a') as f:
                          f.write('%s\n'%insta_id)
      
          return()
      
      

      【讨论】:

        【解决方案3】:

        这是有效的。 不要更改睡眠时间,因为它们允许滚动条重新加载新的关注者,而无需再次将其滚动回顶部。

            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)
        

        【讨论】:

          【解决方案4】:

          我也一直在尝试找到一种方法来滚动关注者弹出窗口或对话框,但还没有找到一种方法来关注关注者框以使用我通常在 WebDriver 中使用的滚动功能。我通过在单击关注者链接后简单地发送 ARROW_DOWN 键直到它一直向下滚动来解决这个问题,计数列表和完整列表在最后一个 ARROW_DOWN 键之后保持不变。我相信关于对话框的部分是无关紧要的,但是嗯。这是我的代码:

              def listfollowers (instaURL):
          actions = ActionChains(driver)
          assert isinstance(instaURL, object)
          driver.get(instaURL)
          time.sleep(3)  # Let the user actually see something!
          followersbutton = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='followers']")))
          followersbutton.click()
          time.sleep(2)
          dialoguebox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body > div:nth-child(14) > div > div.zZYga > div > div.j6cq2 > ul > div")))
          actions.move_to_element(dialoguebox)
          actions.click()
          actions.perform()
          actions.reset_actions()
          

          ###Below 是您列出关注者并滚动关注者对话框所需的内容

          followerlist = []
          scrollfollowercount = driver.find_elements_by_class_name("UYK0S")
          while len(followerlist) < len(scrollfollowercount):
          profiles = driver.find_elements_by_class_name("UYK0S") #the followers
              for profile in profiles:
                  profileurl = profile.get_attribute('href')
                  followerlist.append(profileurl)
                  actions.send_keys(Keys.ARROW_DOWN)
                  actions.send_keys(Keys.ARROW_DOWN)
                  actions.send_keys(Keys.ARROW_DOWN)
                  actions.send_keys(Keys.ARROW_DOWN) #included a few times for good measure
                  actions.perform()
                  actions.reset_actions()
                  scrollfollowercount = driver.find_elements_by_class_name("UYK0S")
                  if len(scrollfollowercount) == len(followerlist):
                      break
          print(followerlist)
          

          所以对于我的 MAIN 部分,我有登录位,然后调用我的函数 listfollowers()

              actions = ActionChains(driver)
          driver.get("https://www.instagram.com/kapow.fitness/followers")
          time.sleep(2)
          username = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(1) > div")))
          username.click()
          actions.send_keys("your-username")
          actions.perform()
          password = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(2) > div > div.f0n8F")))
          password.click()
          actions.reset_actions()
          actions.send_keys("your-password")
          actions.send_keys(Keys.RETURN)
          actions.perform()
          time.sleep(2)
          
          listfollowers("https://www.instagram.com/kapow.fitness/")
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多