📜  使用Selenium Python实现 Twitter 自动化

📅  最后修改于: 2022-05-13 01:54:41.413000             🧑  作者: Mango

使用Selenium Python实现 Twitter 自动化

如果你是像我这样认为 Twitter 比 Instagram 好得多的人,那么我可能会为你准备一些东西。我们都知道在 Twitter 上获得追随者可能非常困难,但有时转推优质内容也能获得追随者。显然,您是一个忙碌的人,您没有时间坐在手机或笔记本电脑上阅读和转发内容。很无聊的任务吧?让我们的聪明朋友去做吧。本文围绕如何使用selenium Python自动化推特。

首先,您将需要Python 。你从这里下载Python 。现在,让我们开始编码。首先,创建一个名为Twitter Automation的文件夹,然后将目录更改为新创建的文件夹。现在,创建一个名为requirements.txt的文件,并将这一行添加到其中。

selenium==3.141.0

接下来,打开终端并输入

pip install -r requirements.txt

接下来,您将需要一个chrome 驱动程序。你可以在这里下载。下载完成后,将下载的驱动程序移动到新创建的文件夹Twitter Automation
现在所有的要求都得到了照顾。现在让我们从编码开始。
现在,创建一个名为credentials.txt的文件,并向其中添加以下行。

email: {your twitter email}
password: {your twitter password}

将电子邮件和密码占位符替换为您的 twitter 原始凭据。我正在使用文本文件。也可以使用.env文件,但为了简单起见,我使用的是.txt文件。

接下来,创建另一个名为secrets.py的文件,并向其中添加以下代码行。

Python3
"""
    Add your twitter handle's email and password
    in the credentials.txt file.
    This will be used to automate the login.
"""
 
def get_credentials() -> dict:
    # dictionary for storing credentials
    credentials = dict()
    # reading the text file
    # for credentials
    with open('credentials.txt') as f:
        # iterating over the lines
        for line in f.readlines():
            try:
                # fetching email and password
                key, value = line.split(": ")
            except ValueError:
                # raises error when email and password not supplied
                print('Add your email and password in credentials file')
                exit(0)
            # removing trailing
            # white space and new line
            credentials[key] = value.rstrip(" \n")
    # returning the dictionary containing the credentials
    return credentials


Python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
'''Uncomment the below line when running in linux'''
# from pyvirtualdisplay import Display
import time, os
 
class Twitterbot:
 
    def __init__(self, email, password):
 
        """Constructor
 
        Arguments:
            email {string} -- registered twitter email
            password {string} -- password for the twitter account
        """
 
        self.email = email
        self.password = password
        # initializing chrome options
        chrome_options = Options()
 
        # adding the path to the chrome driver and
        # integrating chrome_options with the bot
        self.bot = webdriver.Chrome(
            executable_path = os.path.join(os.getcwd(), 'chromedriver'),
            options = chrome_options
        )
 
    def login(self):
        """
            Method for signing in the user
            with the provided email and password.
        """
 
        bot = self.bot
        # fetches the login page
        bot.get('https://twitter.com / login')
        # adjust the sleep time according to your internet speed
        time.sleep(3)
 
        email = bot.find_element_by_xpath(
            '//*[@id ="react-root"]/div / div / div[2]/main / div / div / form / div / div[1]/label / div / div[2]/div / input'
        )
        password = bot.find_element_by_xpath(
            '//*[@id ="react-root"]/div / div / div[2]/main / div / div / form / div / div[2]/label / div / div[2]/div / input'
        )
 
        # sends the email to the email input
        email.send_keys(self.email)
        # sends the password to the password input
        password.send_keys(self.password)
        # executes RETURN key action
        password.send_keys(Keys.RETURN)
 
        time.sleep(2)
 
    def like_retweet(self, hashtag):
 
        """
        This function automatically retrieves
        the tweets and then likes and retweets them
 
        Arguments:
            hashtag {string} -- twitter hashtag
        """
 
        bot = self.bot
 
        # fetches the latest tweets with the provided hashtag
        bot.get(
            'https://twitter.com / search?q =% 23' + \
            hashtag+'&src = typed_query&f = live'
        )
 
        time.sleep(3)
 
        # using set so that only unique links
        # are present and to avoid unnecessary repetition
        links = set()
 
        # obtaining the links of the tweets
        for _ in range(100):
            # executing javascript code
            # to scroll the webpage
            bot.execute_script(
                'window.scrollTo(0, document.body.scrollHeight)'
            )
 
            time.sleep(4)
 
            # using list comprehension
            # for adding all the tweets link to the set
            # this particular piece of code might
            # look very complicated but the only reason
            # I opted for list comprehension because is
            # lot faster than traditional loops
            [
                links.add(elem.get_attribute('href'))\
                for elem in bot.find_elements_by_xpath("//a[@dir ='auto']")
            ]
 
        # traversing through the generated links
        for link in links:
            # opens individual links
            bot.get(link)
            time.sleep(4)
 
            try:
                # retweet button selector
                bot.find_element_by_css_selector(
                    '.css-18t94o4[data-testid ="retweet"]'
                ).click()
                # initializes action chain
                actions = ActionChains(bot)
                # sends RETURN key to retweet without comment
                actions.send_keys(Keys.RETURN).perform()
 
                # like button selector
                bot.find_element_by_css_selector(
                    '.css-18t94o4[data-testid ="like"]'
                ).click()
                # adding higher sleep time to avoid
                # getting detected as bot by twitter
                time.sleep(10)
            except:
                time.sleep(2)
 
        # fetches the main homepage
        bot.get('https://twitter.com/')


Python3
import twitterbot as tb
import secrets, sys
 
# fetches the hashtag from command line argument
hashtag = sys.argv[1]
# fetches the credentials dictionary
# using get_credentials function
credentials = secrets.get_credentials()
# initialize the bot with your credentials
bot = tb.Twitterbot(credentials['email'], credentials['password'])
# logging in
bot.login()
# calling like_retweet function
bot.like_retweet(hashtag)


我在内联注释中添加了详细的代码解释,以便更好地理解。现在,让我们创建最重要的文件,它可以发挥所有作用。创建一个名为twitterbot.py的新文件,并向其中添加以下行。

Python3

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
'''Uncomment the below line when running in linux'''
# from pyvirtualdisplay import Display
import time, os
 
class Twitterbot:
 
    def __init__(self, email, password):
 
        """Constructor
 
        Arguments:
            email {string} -- registered twitter email
            password {string} -- password for the twitter account
        """
 
        self.email = email
        self.password = password
        # initializing chrome options
        chrome_options = Options()
 
        # adding the path to the chrome driver and
        # integrating chrome_options with the bot
        self.bot = webdriver.Chrome(
            executable_path = os.path.join(os.getcwd(), 'chromedriver'),
            options = chrome_options
        )
 
    def login(self):
        """
            Method for signing in the user
            with the provided email and password.
        """
 
        bot = self.bot
        # fetches the login page
        bot.get('https://twitter.com / login')
        # adjust the sleep time according to your internet speed
        time.sleep(3)
 
        email = bot.find_element_by_xpath(
            '//*[@id ="react-root"]/div / div / div[2]/main / div / div / form / div / div[1]/label / div / div[2]/div / input'
        )
        password = bot.find_element_by_xpath(
            '//*[@id ="react-root"]/div / div / div[2]/main / div / div / form / div / div[2]/label / div / div[2]/div / input'
        )
 
        # sends the email to the email input
        email.send_keys(self.email)
        # sends the password to the password input
        password.send_keys(self.password)
        # executes RETURN key action
        password.send_keys(Keys.RETURN)
 
        time.sleep(2)
 
    def like_retweet(self, hashtag):
 
        """
        This function automatically retrieves
        the tweets and then likes and retweets them
 
        Arguments:
            hashtag {string} -- twitter hashtag
        """
 
        bot = self.bot
 
        # fetches the latest tweets with the provided hashtag
        bot.get(
            'https://twitter.com / search?q =% 23' + \
            hashtag+'&src = typed_query&f = live'
        )
 
        time.sleep(3)
 
        # using set so that only unique links
        # are present and to avoid unnecessary repetition
        links = set()
 
        # obtaining the links of the tweets
        for _ in range(100):
            # executing javascript code
            # to scroll the webpage
            bot.execute_script(
                'window.scrollTo(0, document.body.scrollHeight)'
            )
 
            time.sleep(4)
 
            # using list comprehension
            # for adding all the tweets link to the set
            # this particular piece of code might
            # look very complicated but the only reason
            # I opted for list comprehension because is
            # lot faster than traditional loops
            [
                links.add(elem.get_attribute('href'))\
                for elem in bot.find_elements_by_xpath("//a[@dir ='auto']")
            ]
 
        # traversing through the generated links
        for link in links:
            # opens individual links
            bot.get(link)
            time.sleep(4)
 
            try:
                # retweet button selector
                bot.find_element_by_css_selector(
                    '.css-18t94o4[data-testid ="retweet"]'
                ).click()
                # initializes action chain
                actions = ActionChains(bot)
                # sends RETURN key to retweet without comment
                actions.send_keys(Keys.RETURN).perform()
 
                # like button selector
                bot.find_element_by_css_selector(
                    '.css-18t94o4[data-testid ="like"]'
                ).click()
                # adding higher sleep time to avoid
                # getting detected as bot by twitter
                time.sleep(10)
            except:
                time.sleep(2)
 
        # fetches the main homepage
        bot.get('https://twitter.com/')

现在,是时候编写我们的驱动程序脚本了。为此,请创建一个名为main.py的文件并向其中添加以下行。

Python3

import twitterbot as tb
import secrets, sys
 
# fetches the hashtag from command line argument
hashtag = sys.argv[1]
# fetches the credentials dictionary
# using get_credentials function
credentials = secrets.get_credentials()
# initialize the bot with your credentials
bot = tb.Twitterbot(credentials['email'], credentials['password'])
# logging in
bot.login()
# calling like_retweet function
bot.like_retweet(hashtag)

现在,我们完成了代码。让我们通过在终端中运行以下命令来调用我们的驱动程序脚本。

python main.py {hashtag}

只需将主题标签占位符替换为任何趋势主题标签,例如,您可以尝试

python main.py python3

这将喜欢并转发 100 条带有标签Python的推文。您可以在下面的视频中查看它的表现。
所以你有它。继续尝试,不要增加推文的数量,因为推特有每日推文限制。