【问题标题】:Python Selenium Instagram BotPython Selenium Instagram 机器人
【发布时间】:2020-12-14 21:39:44
【问题描述】:

我现在正在构建 Instagram 机器人,我有一个问题。 我希望机器人继续点击我提要中的图片。 我做了这样的事情:

self.driver.find_element_by_css_selector('article._8Rm4L:nth-child(1) > div:nth-child(3) > section:nth-child(1) > span:nth-child(1) > button:nth-child(1)').click()
time.sleep(3)
self.driver.find_element_by_css_selector('article._8Rm4L:nth-child(2) > div:nth-child(3) > section:nth-child(1) > span:nth-child(1) > button:nth-child(1)').click()
time.sleep(3)

而且一直这样,有什么办法比写 1,2,3,4 更容易...

【问题讨论】:

    标签: python instagram bots


    【解决方案1】:

    您必须为其添加滚动才能继续喜欢供稿中的图像

    我的代码:

             SCROLL_PAUSE_TIME = 0.5
                 # Get scroll height
                  last_height = self.driver.execute_script("return 
                  document.body.scrollHeight")
                  while true:
                 # Scroll down to bottom
    
    
                self.driver.execute_script("window.scrollTo
               (0,document.body.scrollHeight);")
    # Wait to load page
    sleep(SCROLL_PAUSE_TIME)
    # Calculate new scroll height and compare with last scroll height
    new_height = self.driver.execute_script("return document.body.scrollHeight")
    
    last_height = new_height
    

    【讨论】:

    • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。能否请您修复一下缩进。
    【解决方案2】:

    最好使用 Xpath => find_element_by_xpath() 或者 您可以使用 selenium IDE 记录每件事并将其保存为 python 文件以进行编辑。

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 2022-01-02
      • 2021-07-23
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2020-01-22
      • 2021-10-01
      相关资源
      最近更新 更多