【问题标题】:Automating following action in Instagram using Python使用 Python 在 Instagram 中自动执行以下操作
【发布时间】:2021-01-24 08:50:32
【问题描述】:

我正在编写一个机器人来自动执行 Instagram 中的以下操作。以下代码确实适用于拳头 5X 追随者,但在那之后,即使 Selenium 确实单击了关注按钮,按钮的状态根本不会改变。谁能发现我的代码中的任何错误?

# Import requiements
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Create our class
class InstagramBot:
    def __init__(self, username, password):
        self.username = '<my user name>'
        self.password = '<my password>'
        self.bot = webdriver.Firefox(executable_path = '<Driver Path>') 

    # Function will log us in to Instagram
    def login(self):
        bot = self.bot
        # Navigate to the Instagram login page
        bot.get('https://www.instagram.com/accounts/login/')
        time.sleep(3)

        # Find the email and password boxes, enter login credentials
        email = bot.find_element_by_name('username').send_keys(self.username)
        password = bot.find_element_by_name('password').send_keys(self.password)

        # Wait for 1 second then press ENTER
        time.sleep(1)
        bot.find_element_by_name('password').send_keys(Keys.RETURN)

        # Wait 3 second while the post-login page loads
        time.sleep(3)


    def followTheirFollowers(self, number_to_follow):
        bot = self.bot
        
        bot.get('https://instagram.com/' + '<Target Account Name>')
        time.sleep(5)
        
        bot.find_element_by_xpath('//a[@href="/' + '<Target Account Name>' + '/follower/"]').click()
        time.sleep(2)
        
        j = 1
        
        while j <= number_to_follow:
            follow = bot.find_elements_by_xpath("//button[(text() = 'Follow')]")
            time.sleep(2)

            i = 1

            for follower in follow:
                if(i != 1):
                    bot.execute_script("arguments[0].click();", follower)
                    time.sleep(2)

                if(i > number_to_follow):
                    break

                i+=1
            
            j += 1
            popup = bot.find_element_by_class_name('isgrP')
            bot.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', popup)
            time.sleep(2)



insta = InstagramBot('USERNAME', 'PASSWORD')
insta.login()
insta.followTheirFollowers(5)

【问题讨论】:

  • 您是否尝试过手动按下该按钮,以查看在发现脚本无法更改状态时是否可以更改状态?
  • 是的,我确实尝试在通过运行脚本打开的浏览器上执行此操作,但它也失败了。但是,当我单独打开另一个时,它可以工作。
  • 我认为 Instagram 有点禁止或检测汽车行为;我也想想办法绕过这个禁令。

标签: python web-scraping instagram


【解决方案1】:

我认为这里的问题很简单。代码本身看起来不错。

在底部您正在运行函数insta.followTheirFollowers(5)followTheirFollowers() 中的参数是number_to_follow,这意味着您只告诉机器人执行此操作五次。如果您更改此数字,机器人应该会执行五次以上。

【讨论】:

  • 我确实尝试将其设为 2000,但仍然失败。
  • 跟随五次后是否收到错误消息?
【解决方案2】:

如果您的代码在前几个案例中正常工作,然后停止工作(刷新页面时不会反映更改),很可能是 Instagram 机器人检测算法在起作用。您可以随机化睡眠时间,例如time.sleep(3 + random.random()),以模仿人类的点击行为。你应该可以用这个来处理一些额外的情况。

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多