编程

使用 Python 自动化 Instagram 消息

299 2023-11-10 21:47:00

在本文中,我们将了解如何向任意数量的人发送一条消息。我们只需要提供一个用户列表。我们将使用selenium来完成这项任务。

需要的包裹

  • Selenium:它是一个自动化 Web 浏览器的开源工具。它提供了一个单一界面,让您可以使用 Ruby、 Java、NodeJS、 PHP、Perl、 Python和 C# 等编程语言编写测试脚本。我个人更喜欢Python ,因为在Python中编写代码非常容易。然后,浏览器驱动程序在您设备上的浏览器实例上执行这些脚本。要安装此模块,请在终端中运行此命令。
pip install selenium
  • webdriver-manager :它提供了一种自动管理不同浏览器驱动程序的方法。要安装此模块,请在终端中运行此命令。
pip install webdriver-manager
  • ChromeDriver: ChromeDriver 是Selenium WebDriver 用来控制 Chrome 的一个单独的可执行文件。
  • 谷歌浏览器
     

第 1 步:导入所有必要的库。

Python3

from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager

Python3

driver = webdriver.Chrome(ChromeDriverManager().install())

Python3

# you can take any valid username
audience = [ 'sundarpichai','virat.kholi','rudymancuso']
message = "testing of a bot"

Python3

class bot:
  
    def __init__(self, username, password, audience, message):
        
        # initializing the username
        self.username = username
          
        # initializing the password
        self.password = password
          
        # passing the list of user or initializing
        self.user = user
          
        # passing the message of user or initializing
        self.message = message
          
        # initializing the base url.
        self.base_url = 'https://www.instagram.com/'
          
        # here it calls the driver to open chrome web browser.
        self.bot = driver
          
        # initializing the login function we will create
        self.login() 

Python3

def login(self):
  
    self.bot.get(self.base_url)
      
    # ENTERING THE USERNAME FOR LOGIN INTO INSTAGRAM
    enter_username = WebDriverWait(self.bot, 20).until(
        expected_conditions.presence_of_element_located((By.NAME, 'username')))
  
    enter_username.send_keys(self.username)
      
    # ENTERING THE PASSWORD FOR LOGIN INTO INSTAGRAM
    enter_password = WebDriverWait(self.bot, 20).until(
        expected_conditions.presence_of_element_located((By.NAME, 'password')))
    enter_password.send_keys(self.password)
  
    # RETURNING THE PASSWORD and login into the account
    enter_password.send_keys(Keys.RETURN)
    time.sleep(5)

Python3

# first pop-up box
self.bot.find_element_by_xpath(
    '//*[@id="react-root"]/section/main/div/div/div/div/button').click()
time.sleep(3)
  
# 2nd pop-up box
self.bot.find_element_by_xpath(
    '/html/body/div[4]/div/div/div/div[3]/button[2]').click()
  
time.sleep(4)

Python3

# this will click on message(direct) option.
self.bot.find_element_by_xpath(
    '//a[@class="xWeGp"]/*[name()="svg"][@aria-label="Direct"]').click()
  
time.sleep(3)

Python3

# This will click on the pencil icon as shown in the figure.
self.bot.find_element_by_xpath(
    '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/button').click()
time.sleep(2)

Python3

for i in user:
  
    # enter the username
    self.bot.find_element_by_xpath(
        '/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input').send_keys(i)
    time.sleep(2)
  
    # click on the username
    self.bot.find_element_by_xpath(
        '/html/body/div[4]/div/div/div[2]/div[2]/div').click()
    time.sleep(2)
  
    # next button
    self.bot.find_element_by_xpath(
        '/html/body/div[4]/div/div/div[1]/div/div[2]/div/button').click()
    time.sleep(2)
  
    # click on message area
    send = self.bot.find_element_by_xpath(
        '/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
  
    # types message
    send.send_keys(self.message)
    time.sleep(1)
  
    # send message
    send.send_keys(Keys.RETURN)
    time.sleep(2)
  
    # clicks on pencil icon
    self.bot.find_element_by_xpath(
        '/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button').click()
    time.sleep(2)
    # here will take the next username from the user's list.

Python3

def init():
    
    # you can even enter your personal account.
    bot('username', 'password', user, message_)
    input("DONE")

Python3

# calling this function will message everyone's
# that is on your user's list...:)
init()

Python3

# importing module
from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
  
driver = webdriver.Chrome(ChromeDriverManager().install())
  
# enter receiver user name
user = ['User_name', 'User_name ']
message_ = ("final test")
  
  
class bot:
    def __init__(self, username, password, user, message):
        self.username = username
        self.password = password
        self.user = user
        self.message = message
        self.base_url = 'https://www.instagram.com/'
        self.bot = driver
        self.login()
  
    def login(self):
        self.bot.get(self.base_url)
  
        enter_username = WebDriverWait(self.bot, 20).until(
            expected_conditions.presence_of_element_located((By.NAME, 'username')))
        enter_username.send_keys(self.username)
        enter_password = WebDriverWait(self.bot, 20).until(
            expected_conditions.presence_of_element_located((By.NAME, 'password')))
        enter_password.send_keys(self.password)
        enter_password.send_keys(Keys.RETURN)
        time.sleep(5)
  
        # first pop-up
        self.bot.find_element_by_xpath(
            '//*[@id="react-root"]/section/main/div/div/div/div/button').click()
        time.sleep(3)
  
        # 2nd pop-up
        self.bot.find_element_by_xpath(
            '/html/body/div[4]/div/div/div/div[3]/button[2]').click()
        time.sleep(4)
  
        # direct button
        self.bot.find_element_by_xpath(
            '//a[@class="xWeGp"]/*[name()="svg"][@aria-label="Direct"]').click()
        time.sleep(3)
  
        # clicks on pencil icon
        self.bot.find_element_by_xpath(
            '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/button').click()
        time.sleep(2)
        for i in user:
  
            # enter the username
            self.bot.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input').send_keys(i)
            time.sleep(2)
  
            # click on the username
            self.bot.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[2]/div[2]/div').click()
            time.sleep(2)
  
            # next button
            self.bot.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[1]/div/div[2]/div/button').click()
            time.sleep(2)
  
            # click on message area
            send = self.bot.find_element_by_xpath(
                '/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
  
            # types message
            send.send_keys(self.message)
            time.sleep(1)
  
            # send message
            send.send_keys(Keys.RETURN)
            time.sleep(2)
  
            # clicks on direct option or pencl icon
            self.bot.find_element_by_xpath(
                '/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button').click()
            time.sleep(2)
  
  
def init():
    bot('username', 'password', user, message_)
  
    # when our program ends it will show "done".
    input("DONE")
  
  
# calling the function
init()

第 2 步:在此代码中使用 chrome 将始终有效(无需担心更新 chromedriver 版本)

Python3

driver = webdriver.Chrome(ChromeDriverManager().install())

第 3 步:在声明一个类之前,我们将创建一个用户列表(此代码中的任何用户数量,我使用三个用户。)在该列表中,您必须发送您想要发送消息的人的用户名和我们要发送给所有用户的消息。

Python3

# you can take any valid username
audience = [ 'sundarpichai','virat.kholi','rudymancuso']
message = "testing of a bot"

第 4 步:这里我正在创建一个类函数,所有代码都将写在这个类中,所以最后,我们只需要调用这个类。让我们创建一个可以在从类创建对象时调用的 __init__函数,__init__ 将提供初始化类属性所需的访问权限。

Python3

class bot:
  
    def __init__(self, username, password, audience, message):
        
        # initializing the username
        self.username = username
          
        # initializing the password
        self.password = password
          
        # passing the list of user or initializing
        self.user = user
          
        # passing the message of user or initializing
        self.message = message
          
        # initializing the base url.
        self.base_url = 'https://www.instagram.com/'
          
        # here it calls the driver to open chrome web browser.
        self.bot = driver
          
        # initializing the login function we will create
        self.login()

第 5 步:创建一个登录函数,其中将编写所有步骤

Python3

def login(self):
  
    self.bot.get(self.base_url)
      
    # ENTERING THE USERNAME FOR LOGIN INTO INSTAGRAM
    enter_username = WebDriverWait(self.bot, 20).until(
        expected_conditions.presence_of_element_located((By.NAME, 'username')))
  
    enter_username.send_keys(self.username)
      
    # ENTERING THE PASSWORD FOR LOGIN INTO INSTAGRAM
    enter_password = WebDriverWait(self.bot, 20).until(
        expected_conditions.presence_of_element_located((By.NAME, 'password')))
    enter_password.send_keys(self.password)
  
    # RETURNING THE PASSWORD and login into the account
    enter_password.send_keys(Keys.RETURN)
    time.sleep(5)

注意:要找到 XPath,请右键单击元素,然后您会在 Web 浏览器上看到检查选项。

选择 xpath

第 6 步:处理弹出框 - 当您登录 Instagram 时,您会看到两个弹出框,如图所示。第一个将询问您是否要保存信息,它现在不会点击。第二个将询问您是否关闭通知,因此我们将再次单击 not now,如给定代码所示。

Python3

# first pop-up box
self.bot.find_element_by_xpath(
    '//*[@id="react-root"]/section/main/div/div/div/div/button').click()
time.sleep(3)
  
# 2nd pop-up box
self.bot.find_element_by_xpath(
    '/html/body/div[4]/div/div/div/div[3]/button[2]').click()
  
time.sleep(4)

第一个弹出框

第二个弹出框

第7步:点击右上角的消息选项

消息按钮(直接按钮)

注意:这将单击消息(直接)选项。

Python3

# this will click on message(direct) option.
self.bot.find_element_by_xpath(
    '//a[@class="xWeGp"]/*[name()="svg"][@aria-label="Direct"]').click()
  
time.sleep(3)

第 8 步:单击此处的铅笔选项,我们输入用户名。

Python3

# This will click on the pencil icon as shown in the figure.
self.bot.find_element_by_xpath(
    '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/button').click()
time.sleep(2)

第 9 步:这是代码的主要部分,我们从列表中获取用户名搜索并发送消息给该人。此过程在获取用户名的循环中。

Python3

for i in user:
  
    # enter the username
    self.bot.find_element_by_xpath(
        '/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input').send_keys(i)
    time.sleep(2)
  
    # click on the username
    self.bot.find_element_by_xpath(
        '/html/body/div[4]/div/div/div[2]/div[2]/div').click()
    time.sleep(2)
  
    # next button
    self.bot.find_element_by_xpath(
        '/html/body/div[4]/div/div/div[1]/div/div[2]/div/button').click()
    time.sleep(2)
  
    # click on message area
    send = self.bot.find_element_by_xpath(
        '/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
  
    # types message
    send.send_keys(self.message)
    time.sleep(1)
  
    # send message
    send.send_keys(Keys.RETURN)
    time.sleep(2)
  
    # clicks on pencil icon
    self.bot.find_element_by_xpath(
        '/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button').click()
    time.sleep(2)
    # here will take the next username from the user's list.

第10步:我们终于完成了代码。是时候创建一个函数了,我们将在其中传递我们要登录的帐户的用户名和密码,并在此处调用我们的类。

Python3

def init():
    
    # you can even enter your personal account.
    bot('username', 'password', user, message_)
    input("DONE")

第 11 步:让我们调用我们的函数

Python3

# calling this function will message everyone's
# that is on your user's list...:)
init()

让我们把所有的代码放在一起

Python3

# importing module
from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
  
driver = webdriver.Chrome(ChromeDriverManager().install())
  
# enter receiver user name
user = ['User_name', 'User_name ']
message_ = ("final test")
  
  
class bot:
    def __init__(self, username, password, user, message):
        self.username = username
        self.password = password
        self.user = user
        self.message = message
        self.base_url = 'https://www.instagram.com/'
        self.bot = driver
        self.login()
  
    def login(self):
        self.bot.get(self.base_url)
  
        enter_username = WebDriverWait(self.bot, 20).until(
            expected_conditions.presence_of_element_located((By.NAME, 'username')))
        enter_username.send_keys(self.username)
        enter_password = WebDriverWait(self.bot, 20).until(
            expected_conditions.presence_of_element_located((By.NAME, 'password')))
        enter_password.send_keys(self.password)
        enter_password.send_keys(Keys.RETURN)
        time.sleep(5)
  
        # first pop-up
        self.bot.find_element_by_xpath(
            '//*[@id="react-root"]/section/main/div/div/div/div/button').click()
        time.sleep(3)
  
        # 2nd pop-up
        self.bot.find_element_by_xpath(
            '/html/body/div[4]/div/div/div/div[3]/button[2]').click()
        time.sleep(4)
  
        # direct button
        self.bot.find_element_by_xpath(
            '//a[@class="xWeGp"]/*[name()="svg"][@aria-label="Direct"]').click()
        time.sleep(3)
  
        # clicks on pencil icon
        self.bot.find_element_by_xpath(
            '//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div/button').click()
        time.sleep(2)
        for i in user:
  
            # enter the username
            self.bot.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[2]/div[1]/div/div[2]/input').send_keys(i)
            time.sleep(2)
  
            # click on the username
            self.bot.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[2]/div[2]/div').click()
            time.sleep(2)
  
            # next button
            self.bot.find_element_by_xpath(
                '/html/body/div[4]/div/div/div[1]/div/div[2]/div/button').click()
            time.sleep(2)
  
            # click on message area
            send = self.bot.find_element_by_xpath(
                '/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
  
            # types message
            send.send_keys(self.message)
            time.sleep(1)
  
            # send message
            send.send_keys(Keys.RETURN)
            time.sleep(2)
  
            # clicks on direct option or pencl icon
            self.bot.find_element_by_xpath(
                '/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[1]/div/div[3]/button').click()
            time.sleep(2)
  
  
def init():
    bot('username', 'password', user, message_)
  
    # when our program ends it will show "done".
    input("DONE")
  
  
# calling the function
init()