【python+selenium】全自动点赞评论,附源码

更新时间:2021-03-10 13:59:22 点击次数:1251次
源码(注释齐全)
#!/usr/bin/python3
# _*_coding=utf-8 _*_
# @author junwei
# @date 2021/2/28 16:39
# description CSDN自动评论
import time
import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class CSDNComment(object):
    def __init__(self, headless):
        if headless is True:
            # 无界面运行
            chrome_options = Options()
            chrome_options.add_argument('--headless')
            chrome_options.add_argument('--disable-gpu')
            self.driver = webdriver.Chrome(options=chrome_options)
        else:
            # 界面全屏运行
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
        # 选择通过账号密码进行登录
        login_url = "https://passport.csdn.net/login?code=public"
        self.driver.get(login_url)
        self.driver.find_element_by_css_selector(
            "#app > div > div > div.main > div.main-login > div.main-select > ul > li:nth-child(2) > a").click()  # 点击账号密码登录
        self.driver.implicitly_wait(2)
    # 登录CSDN
    def login(self, userName, pwd):
        self.driver.find_element_by_id("all").send_keys(userName)  # 传入userName
        self.driver.find_element_by_id("password-number").send_keys(pwd)  # 传入password
        time.sleep(2)
        self.driver.find_element_by_css_selector(
            "#app > div > div > div.main > div.main-login > div.main-process-login > div > div:nth-child(6) > div > button").click()  # 点击登录
        time.sleep(2)
        if self.driver.current_url == "https://www.csdn.net/":
            print("login successful, and then start to comment automatically")
        else:
            raise Exception("login failed, please check your info")
    # 获取排行榜文章
    def auto_opt(self):
        self.driver.find_element_by_css_selector(
            "#floor-nav_62 > div > div > div.index_nav_left > ul > li:nth-child(3) > a").click()  # 进入排行榜
        # 确保切换到了排行榜标签页
        handles = self.driver.window_handles
        time.sleep(1)
        self.driver.switch_to.window(handles[1])
        # [综合榜]
        self.scrollTop(50000, 4)  # 下拉整个页面到最底三次,让页面加载出全部的排行榜文章(否则只有25篇)
        # 获取全部文章
        all_articles = self.driver.find_elements_by_xpath(
            '//*[@id="floor-rank_460"]/div/div[2]/div[1]/div/div[2]/div/div[1]/a')
        '''
        注意:一定要确保切换到操作的窗口句柄,虽然可视化显示是在A页面上的,但很可能实际没获取到
        '''
        print('现在开始"综合"榜自动评论点赞')
        for article in all_articles:
            # 获取标题
            title = article.text
            # 开一个新标签页
            js = "window.open('%s')" % article.get_attribute("href")
            self.driver.execute_script(js)
            # 确保切换到了排行榜标签页
            handles = self.driver.window_handles
            time.sleep(1)
            self.driver.switch_to.window(handles[2])
            # 执行自动点赞,评论操作
            time.sleep(2)
            self.comment_thumbsUp()
            print("评论点赞二连成功,文章标题:%s" % title)
            # 关闭该文章页面
            time.sleep(2)
            self.driver.close()
            time.sleep(1)
            # 确保回到排行榜标签页
            handles = self.driver.window_handles
            time.sleep(1)
            self.driver.switch_to.window(handles[1])
            # 防止被判定为脚本
            time.sleep(10)
        # [其他分类榜]
        # 获取全部榜单类型
        category_lists = self.driver.find_elements_by_xpath(
            '//*[@id="floor-rank_460"]/div/div[2]/div[1]/div/div[1]/div[1]/ul/li')
        for category in category_lists:
            category.click()
            category_text = category.text
            print('当前进入"%s"榜' % category_text)
            self.scrollTop(50000, 4)
            # 获取全部文章
            all_category_articles = self.driver.find_elements_by_xpath(
                '//*[@id="floor-rank_460"]/div/div[2]/div[1]/div/div[2]/div/div[1]/a')
            print('现在开始"%s"榜自动评论点赞' % category_text)
            for category_article in all_category_articles:
                title = category_article.text
                js = "window.open('%s')" % category_article.get_attribute("href")
                self.driver.execute_script(js)
                handles = self.driver.window_handles
                time.sleep(1)
                self.driver.switch_to.window(handles[2])
                # 执行自动点赞,评论操作
                time.sleep(2)
                self.comment_thumbsUp()
                print("评论点赞二连成功,文章标题:%s" % title)
                # 关闭该文章页面
                time.sleep(2)
                self.driver.close()
                time.sleep(1)
                # 确保回到排行榜标签页
                handles = self.driver.window_handles
                time.sleep(1)
                self.driver.switch_to.window(handles[1])
                # 防止被判定为脚本
                time.sleep(10)
    # 循环垂直下拉滚动
    def scrollTop(self, y, range_time):
        for i in range(range_time):
            js = "var q = document.documentElement.scrollTop = %s" % (range_time * y)
            self.driver.execute_script(js)
            time.sleep(1)
    # 自定义评论的内容
    def comment_content(self):
        comments = ["博客很全面,很仔细,点赞", "有所收获,感谢", "学习了", "学起来,坚持~欢迎回访一起交流!", "mark~"]
        comment = comments[random.randint(0, len(comments) - 1)]
        return comment
    def comment_thumbsUp(self):
        # 评论
        self.driver.find_element_by_id("comment_content").send_keys(self.comment_content())
        time.sleep(2)
        self.driver.find_element_by_xpath('//*[@id="rightBox"]/a/input').click()
        # 点赞
        self.driver.find_element_by_id("is-like-span").click()
if __name__ == '__main__':
    autoComment = CSDNComment(False)  # True代表无界面运行,False代表界面全屏运行
    userName = input("please input your userName:")
    pwd = input("please input your password:")
    autoComment.login(userName, pwd)
    # autoComment.login("userName", "pwd")
    autoComment.auto_opt()

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!