在Python中使用Selenium在 Instagram 上发送直接消息
在本文中,我们将学习如何在不进行任何手动操作的情况下向 Instagram 上的用户发送直接消息。我们将使用selenium模块来完成这项任务。
要求:
- Chrome 浏览器的 Chrome 驱动程序 (https://chromedriver.chromium.org/) 或 Firefox 的 Gecko 驱动程序 (https://github.com/mozilla/geckodriver/releases)
- Selenium包。要安装此类型,请在终端中输入以下命令。
pip install selenium
注意:有关更多信息,请参阅如何在Python中安装Selenium
方法:
步骤1:导入模块并输入登录信息以及您要发送消息的用户的用户名。
Python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.common.exceptions
import time
import random
# Login Credentials
username = input('Enter your Username ')
password = input('Enter your Password ')
url = 'https://instagram.com/' + input('Enter username of user whome you want to send message')
Python3
def path():
global chrome
# starts a new chrome session
chrome = webdriver.Chrome() # Add path if required
Python3
def url_name(url):
chrome.get(url)
# adjust sleep if you want
time.sleep(4)
Python3
def login(username, your_password):
log_but = chrome.find_element_by_class_name("L3NKy")
time.sleep(2)
log_but.click()
time.sleep(4)
# finds the username box
usern = chrome.find_element_by_name("username")
# sends the entered username
usern.send_keys(username)
# finds the password box
passw = chrome.find_element_by_name("password")
# sends the entered password
passw.send_keys(your_password)
# press enter after sending password
passw.send_keys(Keys.RETURN)
time.sleep(5.5)
# Finding Not Now button
notk = chrome.find_element_by_class_name("yWX7d")
notk.click()
time.sleep(3)
Python3
def send_message():
# Find message button
message = chrome.find_element_by_class_name('_862NM ')
message.click()
time.sleep(2)
chrome.find_element_by_class_name('HoLwm ').click()
time.sleep(1)
l = ['hello', 'Hi', 'How are You', 'Hey', 'Bro whats up']
for x in range(10):
mbox = chrome.find_element_by_tag_name('textarea')
mbox.send_keys(random.choice(l))
mbox.send_keys(Keys.RETURN)
time.sleep(1.2)
Python3
path()
time.sleep(1)
url_name(url)
login(username, password)
send_message()
chrome.close()
第 2 步:初始化 Firefox 或 chrome 会话的函数。您可能需要将路径添加到 Web 驱动程序。 Chrome函数,这取决于您的安装。
蟒蛇3
def path():
global chrome
# starts a new chrome session
chrome = webdriver.Chrome() # Add path if required
第3步:输入页面URL的函数
蟒蛇3
def url_name(url):
chrome.get(url)
# adjust sleep if you want
time.sleep(4)
第 4 步:登录 Instagram 的函数
蟒蛇3
def login(username, your_password):
log_but = chrome.find_element_by_class_name("L3NKy")
time.sleep(2)
log_but.click()
time.sleep(4)
# finds the username box
usern = chrome.find_element_by_name("username")
# sends the entered username
usern.send_keys(username)
# finds the password box
passw = chrome.find_element_by_name("password")
# sends the entered password
passw.send_keys(your_password)
# press enter after sending password
passw.send_keys(Keys.RETURN)
time.sleep(5.5)
# Finding Not Now button
notk = chrome.find_element_by_class_name("yWX7d")
notk.click()
time.sleep(3)
第 5 步:在用户个人资料页面找到消息按钮,然后向用户发送随机消息
蟒蛇3
def send_message():
# Find message button
message = chrome.find_element_by_class_name('_862NM ')
message.click()
time.sleep(2)
chrome.find_element_by_class_name('HoLwm ').click()
time.sleep(1)
l = ['hello', 'Hi', 'How are You', 'Hey', 'Bro whats up']
for x in range(10):
mbox = chrome.find_element_by_tag_name('textarea')
mbox.send_keys(random.choice(l))
mbox.send_keys(Keys.RETURN)
time.sleep(1.2)
第 6 步:调用函数
蟒蛇3
path()
time.sleep(1)
url_name(url)
login(username, password)
send_message()
chrome.close()
就是这样!此脚本会自动向您所爱的人发送消息。您可以通过修改此脚本来做很多事情,例如安排自动消息、向大量用户发送消息等等。
输出: