0


使用selenium 对在csdn自动评论代码实现

通过此脚本可实现自动评论功能。

import time

from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.chrome.options import Options

deffunction():
    options = Options()# cache_dir = os.path.join(os.path.dirname(__file__),"disk-cache")# options.add_argument("--disable-cache")# options.add_argument(f"--disk-cache-dir={cache_dir}")# options.add_argument(f"user-data-dir={cache_dir}")
    options.page_load_strategy ='none'
    driver = webdriver.Chrome(options)
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get('https://passport.csdn.net/login')# time.sleep(1)
    driver.find_element('xpath','/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[1]/span[4]').click()# driver.find_element('xpath','//span[@class="tabs-active"]').click()# 输入账户
    driver.find_element('xpath','//input[@autocomplete="username"]').send_keys("xxx")# 输入密码
    driver.find_element('xpath','//input[@autocomplete="current-password"]').send_keys("xxxx")
    driver.find_element('xpath','/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/p/div/i').click()
    driver.find_element('xpath','//button[text()="登录"]').click()# time.sleep(1)
    driver.find_element('xpath','//li[@title="阅读深度、前沿文章"]').click()
    driver.find_element('xpath','//div[@class="blog-nav-box"]/ul/li[6]').click()for i inrange(1,40):
        eles = driver.find_element('xpath','//div[@class="content"]/a')
        text = eles.text
        print(text)
        eles.click()
        windows_handls = driver.window_handles
        print(windows_handls)# time.sleep(1)
        driver.switch_to.window(windows_handls[1])
        driver.find_element('xpath','//div[@class="toolbox-middle"]/ul/li[@class="tool-item tool-item-size tool-active tool-item-comment"]').click()
        time.sleep(1)try:
            driver.find_element('xpath','//textarea[@name="comment_content"]').send_keys(f"这篇关于{text}的文章写的太好了,完全是我想要学习的,大佬的文章让我对这领域的技术问题有了更深入的了解,尤其是大佬提到的那些“坑点”,我相信能够在实际应用中避免或解决很多问题。谢谢大佬的分享,期待大佬的更多精彩文章,让我们共同学习、进步")# driver.find_element('xpath', '//textarea[@name="comment_content"]').send_keys(# "非常喜欢这篇博客!博主的写作风格简洁明了,让人一目了然。文章内容丰富,涵盖了很多实用的知识点,对我来说非常有帮助。尤其是博主在文章中提供了很多实际操作的步骤和技巧,让我能够更好地应用所学的知识。同时,博主的配图也很精美,更加生动地展示了文章中的内容。总之,非常感谢博主的分享,期待博主能够继续输出这样优质的好文。")
            driver.find_element('xpath','//input[@class="btn-comment btn-comment-input"]').click()
            time.sleep(30)except Exception as e:print("Exception"+str(e))# time.sleep(1)
        driver.close()
        driver.switch_to.window(windows_handls[0])# time.sleep(1)
        driver.refresh()
    driver.quit()if __name__ =='__main__':
       function()## if os.path.exists(cache_dir):#     os.remove(cache_dir)

本文转载自: https://blog.csdn.net/weixin_42439274/article/details/136615754
版权归原作者 一个双鱼座的测开 所有, 如有侵权,请联系我们删除。

“使用selenium 对在csdn自动评论代码实现”的评论:

还没有评论