优化 Selenium 脚本以加快执行速度

问题描述 投票:0回答:1

我使用 Selenium 开发了一个 Python 脚本,用于在 Twitter 上自动执行任务,包括登录、发布带有图像的推文、喜欢自己的推文、转发和注销。虽然脚本运行良好,但我注意到进程相对较慢。我正在寻找优化脚本问题的方法,以加快执行速度。

我已经实现了 time.sleep() 来引入延迟,但我愿意接受更好的方法。 我正在使用启用了无头模式的 Firefox WebDriver。 该脚本正在 RTX 3050 上执行。 任何有关如何提高脚本整体效率的见解或建议将不胜感激



User
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys
import time


class TwitterBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        options = Options()
        options.add_argument('--disk-cache-dir=/path/to/cache')
        options.add_argument('--headless')
        self.bot = webdriver.Firefox(options=options)


    def login(self):
        bot = self.bot
        bot.get('https://twitter.com/login')
        time.sleep(3)
        email = bot.find_element(By.XPATH, "//input[@name='text']")
        email.send_keys(self.username)
        email.send_keys(Keys.RETURN)
        time.sleep(3)
        password = bot.find_element(By.XPATH, "//input[@name='password']")
        password.send_keys(self.password)
        password.send_keys(Keys.RETURN)
        time.sleep(4)

    def post_tweet_with_image(self, tweet_text, image_path):
        bot = self.bot
        bot.get('https://twitter.com/compose/tweet')
        time.sleep(3)

        tweet_input = bot.find_element(By.XPATH, "//div[@data-testid='tweetTextarea_0']")
        tweet_input.send_keys(tweet_text)
        time.sleep(2)

        if image_path:
            image_input = bot.find_element(By.XPATH, "//input[@data-testid='fileInput']")
            image_input.send_keys(image_path)
            time.sleep(2)

        tweet_button = bot.find_element(By.XPATH, "//text()[.='Post']/ancestor::div[1]")
        tweet_button.click()
        time.sleep(10)
    def like_own_tweet(self):
        bot = self.bot
    # Go to profile
        profile_button = bot.find_element(By.XPATH, "//span[normalize-space()='Profile']")
        profile_button.click()
        time.sleep(3)
        bot.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        time.sleep(3)
    # Locate the latest tweet on your profile
        tweet = bot.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/div[1]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]")   
        tweet.click()
        time.sleep(3)
    # Like the tweet
        like_button = bot.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/section[1]/div[1]/div[1]/div[1]/div[1]/div[1]/article[1]/div[1]/div[1]/div[3]/div[6]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/*[name()='svg'][1]")
        like_button.click()
        time.sleep(3)
    def retweet_from_user(self, user_handle):
        bot = self.bot
        search_bar = bot.find_element(By.XPATH, "//input[@data-testid='SearchBox_Search_Input']")

        search_bar.clear()
        search_bar.send_keys(f"from:{user_handle}")
        search_bar.send_keys(Keys.RETURN)
        time.sleep(5)
        bot.find_element(By.LINK_TEXT,"Latest").click()
        time.sleep(3)
        # Click on the latest tweet from the user
        tweet = bot.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/section[1]/div[1]/div[1]/div[1]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]/div[2]/div[1]")
        tweet.click()
        time.sleep(3)
        # Retweet the tweet
        retweet_button = bot.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/section[1]/div[1]/div[1]/div[1]/div[1]/div[1]/article[1]/div[1]/div[1]/div[3]/div[6]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/*[name()='svg'][1]")
        retweet_button.click()
        repost_button = bot.find_element(By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/span[1]")
        repost_button.click()
        time.sleep(3)


    def logout(self):
        bot = self.bot
        profile_button = bot.find_element(By.XPATH, "//div[@data-testid='SideNav_AccountSwitcher_Button']")
        profile_button.click()
        time.sleep(2)
        logout_button = bot.find_element(By.XPATH, "//div[@data-testid='AccountSwitcher_Logout_Button']")
        logout_button.click()
        time.sleep(2)

    def close_browser(self):
        self.bot.quit()

if __name__ == "__main__":
    users = [ "write user names and the password here"]

    for user_info in users[:15]:
        username = user_info["username"]
        password = user_info["password"]

        # Create an instance of the TwitterBot
        twitter_bot = TwitterBot(username, password)
        twitter_bot.login()

        # Loop to post multiple tweets
        while True:
            tweet_text = "anything"
            image_path = r"C:\Users\hp\Downloads\anything.jpeg"


            # Post the tweet
            twitter_bot.post_tweet_with_image(tweet_text, image_path)

            # Like and retweet functionality
            break

        # Close the browser session
        twitter_bot.close_browser()
python selenium-webdriver web-scraping firefox browser-automation
1个回答
1
投票

摆脱所有

time.sleep()
...它们是一种不好的做法,会减慢你的脚本速度。将它们替换为
WebDriverWait

如果要单击某个元素,请等待它可单击。

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable(...)).click()

如果您要与某个元素进行交互,例如

.text
.send_keys()
,等待其可见。

wait.until(EC.visibility_of_element_located(...)).send_keys()

例如,

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='text']"))).send_keys(self.username)

有关更多信息和示例,请参阅文档

如果您想要更快的速度并且在一次运行中执行多个任务,您可以添加并行性并将每个操作作为单独的线程运行。我将把它作为练习留给读者,因为它涉及更多,并且如果您不知道自己在做什么,就会存在一些风险。请参阅文档了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.