CSDN 自动评论互动脚本

在这里插入图片描述
在这里插入图片描述

声明

该脚本的目的只是为了提升博客创作效率和博主互动效率,希望大家还是要尊重各位博主的劳动成果。

数据库设计

尽量我们要新建一个数据库csdn_article,再在其中建一个数据表article

-- csdn_article-- article-- 需要进行自动评论的表格信息...
CREATE TABLE `article` (`articleId` bigint NOT NULL COMMENT 'id',`articleDetailUrl` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'url',`articleTitle` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '标题',`nickName` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '昵称',`hotRankScore` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '热度',`comment` int NOT NULL DEFAULT '0' COMMENT '是否评论(0,否,1是)',`like` int NOT NULL DEFAULT '0' COMMENT '是否点赞(0,否,1是)',`insert_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`articleId`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;

代码

import random, time, os, threading
import requests
import pymysql
from dbutils.pooled_db import PooledDB
from schedule import every, repeat, run_pending
import jsonPOOL = PooledDB(creator=pymysql,  # 使用链接数据库的模块maxconnections=20,  # 连接池允许的最大连接数,0和None表示不限制连接数mincached=6,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建maxcached=None,  # 链接池中最多闲置的链接,0和None不限制maxshared=5,blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制setsession=[],  # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]ping=0,  # ping MySQL服务端,检查是否服务可用。host='127.0.0.1', port=3306, user='xxx', password='xxx', database='csdn_article', charset='utf8')def insert_article(articleId, articleDetailUrl, articleTitle, nickName, hotRankScore):print("===【插入文章】运行开始 ===\n")db = POOL.connection()conn = db.cursor()  # 使用cursor()方法获取操作游标sql = "INSERT INTO `article` (`articleId`, `articleDetailUrl`, `articleTitle`, `nickName`, `hotRankScore`) VALUES (%s, %s, %s, %s, %s);"data = (articleId, articleDetailUrl, articleTitle, nickName, hotRankScore)conn.execute(sql, data)    # 使用execute方法执行SQL语句data = db.commit()  # 使用 fetchone() 方法获取一条数据db.close()print("===【插入文章】运行结束 ===\n")return datadef select_is_insert(articleId):print("===【检查文章是否已插入】运行结束 ===\n")db = POOL.connection()conn = db.cursor()  # 使用cursor()方法获取操作游标conn.execute("SELECT COUNT(*) FROM `article` WHERE `articleId` = %s;" % articleId)  # 使用execute方法执行SQL语句data = conn.fetchall()  # 使用 fetchone() 方法获取一条数据db.close()print("===【检查文章是否已插入】运行结束 ===\n")return data[0][0]def select_is_comment():  # 查询没有评论的数据print("===【查询未评论文章】运行开始 ===\n")db = POOL.connection()conn = db.cursor()  # 使用cursor()方法获取操作游标conn.execute(# "SELECT `articleId`,`articleDetailUrl` FROM `article` WHERE `comment` = '0' LIMIT 0, 2;")  # 使用execute方法执行SQL语句"SELECT `articleId`,`articleDetailUrl` FROM `article` WHERE `comment` = '0' LIMIT 0, 10;")data = conn.fetchall()  # 使用 fetchone() 方法获取一条数据db.close()print("===【查询未评论文章】运行结束 ===\n")return datadef update_article(articleId, comment=1):print("===【更新文章评论状态(未评论->已评论)】运行开始 ===\n")db = POOL.connection()conn = db.cursor()  # 使用cursor()方法获取操作游标conn.execute("UPDATE `article` SET  `comment` =%s WHERE `articleId` = %s;" % (comment, articleId))  # 使用execute方法执行SQL语句data = db.commit()  # 使用 fetchone() 方法获取一条数据db.close()print("===【更新文章评论状态(未评论->已评论)】运行结束 ===\n")return datadef comment(articleId):"""评论"""print("===【评论功能】运行开始 ===\n")# time.sleep(5)# user_headers = {#     'Host': 'blog.csdn.net',#     'Connection': 'keep-alive',#     'Content-Length': '75',#     'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',#     'Accept': 'application/json, text/javascript, */*; q=0.01',#     'X-Requested-With': 'XMLHttpRequest',#     'sec-ch-ua-mobile': '?0',#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36',#     'X-Tingyun-Id': 'im-pGljNfnc;r=290154423',#     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',#     'Origin': 'https://blog.csdn.net',#     'Sec-Fetch-Site': 'same-origin',#     'Sec-Fetch-Mode': 'cors',#     'Sec-Fetch-Dest': 'empty',#     'Accept-Encoding': 'gzip, deflate, br',#     'Accept-Language': 'zh-CN,zh;q=0.9',#     'Cookie': 'FCNEC=%5B%5B%22AKsRol-TfGIJmIpEnHn0eT97Nu7oKKAxvs3WgXk1StZO69TpBxsJjDWjOjdFCul5Ii3O_3A84GAna-UgoIdLOhT9t1ozSb_SxwrTauMaHxcri3-TsZ7Ngq8_g2-5tAmAhgDtAGmA4TlPc0n-53j6efyfpC-DQAWmtQ%3D%3D%22%5D%2Cnull%2C%5B%5D%5D; p_uid=U010000; UserName=m0_74120525; UserInfo=f1185dc9432349ef88e6698369cc47e8; UserToken=f1185dc9432349ef88e6698369cc47e8; UserNick=Byyyi%E8%80%80; AU=952; UN=m0_74120525; BT=1697540891864; historyList-new=%5B%5D; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22m0_74120525%22%2C%22scope%22%3A1%7D%7D; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_6637886500-1690368897762-971900!5744*1*m0_74120525; __gads=ID=f3980e3c20261260-22fe288faae2007f:T=1691114901:RT=1700205458:S=ALNI_MZWrzMlHVOHhzMa9RK_63XYBbYOmQ; __gpi=UID=00000c26ce6e47f3:T=1691114901:RT=1700205458:S=ALNI_MYiPc8Avf5S2miZnxyMuopfW5wlrw; pluginId=kfkdboecolemdjodhmhmcibjocfopejo; pluginUUID=10_87822374348-1701002105453-558370; fpv=f0f83d21c22b3f2b76e36cda57cae212; c_adb=1; ins_first_time=1708819983909; pluginVersion=3.0.6; _ga=GA1.1.1471788577.1709786322; _ga_91C47RQLV7=GS1.1.1709786322.1.1.1709786348.0.0.0; uuid_tt_dd=10_21307064330-1711099818774-487527; management_ques=1711433140316; ssxmod_itna=Yq+xgDBDcGYQwx7q0dD=moxCqOG9KQt33OKODlrW4A5D8D6DQeGTT0aYD0Q=ANAi54=A4SYw51A9EQbpImixwBnQTDCPGnDBIwxOADYY8Dt4DTD34DYDioQGIDieDFWqNU8DbxYQDmxAQDzkd9DGfyDYPcYqDg9qDBCodDKqGg8T5D0T5tchGYCDxPwddKVkbDAuhBYGDcD0U3xBdPma5czC98cEUBF6aerhjDqeGuDG6VWqGm98bDCE6Mijv3iOG3IG5NIixaCGDeQBo5C845lWhFLiq1tg4=GBurtxqzU5DA4HMQeD; ssxmod_itna2=Yq+xgDBDcGYQwx7q0dD=moxCqOG9KQt33OKD61tAhD0HpbP03qt8nj6D6GYLkYwDxPMWBk9p+ynr0r0xa96rFN5DGrSjz53biIKGG9=iFmGUnr9Ew1rQXBd8ARCBpdTRT1eEpynnw8DunoKSBTisn0fGtCEK4334Sgv6YAXPVgI=txG204GcDiQLeD==; tfstk=fEfE3AcoiWFU7tWkbhOy3CC6GVdpaIEjx_tWrabkRHx32BilUGITJ__uv3YPbh95d3OHzg7cfQtQpuINaUBtKevkxhxMJEtnqXYkjAYvr3jPyyLkqgjyV9w_GwQpwQqX4SNfJFZs8iCeZ0_MQFTj7oNbGwxCrzFgczhE2V-WrQYHELAg7EYXZDclqhvMoEiHZQjlSP8W8YYnZpvgseTE9Gjlcf8JKka-OUb3sUvemN2xZb5rGp-c7hloP18eZnbHbbckvhw6NN7QYjQ691s2lMNiTi7hngXcj5leB9bNaKC-xPAkEO6XLOrnah1BzKAkQ4cl7Bx9H_A0ic8OIw6l9MurZeCCGL-vQzcJFCfXntjEyrQMtUjJH_Zx9HblksBXg7lMTLjzr0LG8tfRL0DyKFLwcPzipH3o-tgdLdH-epp97na3-YDoDjGSL_aKeYppXF-bWU5..; creative_btn_mp=3; showTip=false; m0_74120525comment_new=1713062290150; c_dl_fpage=/download/xtfnpgy/7381257; c_dl_um=-; c_dl_prid=1713063451124_339522; c_dl_rid=1713063597342_947078; c_dl_fref=https://blog.csdn.net/weixin_42439274/article/details/136615754; dc_session_id=10_1713073125709.575893; toolbar_remind_num=3; referrer_search=1713073149817; firstDie=1; _clck=keeq79%7C2%7Cfkx%7C0%7C1547; dc_sid=1b8584bba792dac064f4cee8e69945ba; csrfToken=zDba7vtMd1lDWbd93RJAfvCn; c_first_ref=default; c_first_page=https%3A//www.csdn.net/; c_segment=7; www_red_day_last=red; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1713011069,1713011345,1713073122,1713077297; creativeSetApiNew=%7B%22toolbarImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20230921102607.png%22%2C%22publishSuccessImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20240229024608.png%22%2C%22articleNum%22%3A51%2C%22type%22%3A2%2C%22oldUser%22%3Atrue%2C%22useSeven%22%3Afalse%2C%22oldFullVersion%22%3Atrue%2C%22userName%22%3A%22m0_74120525%22%7D; c_dsid=11_1713077319222.930056; fe_request_id=1713077415262_8128_2661541; c_pref=https%3A//www.csdn.net/; c_ref=https%3A//so.csdn.net/so/search%3Fspm%3D1000.2115.3001.4498%26q%3D%25E6%2589%25BE%25E4%25B8%258D%25E5%2588%25B0%25E7%25BD%2591%25E7%25AB%2599Cookie%26t%3D%26u%3D; c_utm_term=%E6%89%BE%E4%B8%8D%E5%88%B0%E7%BD%91%E7%AB%99Cookie; _clsk=4e6taa%7C1713077455475%7C10%7C0%7Cd.clarity.ms%2Fcollect; log_Id_click=688; c_utm_relevant_index=3; relevant_index=3; c_utm_medium=distribute.pc_search_result.none-task-blog-2%7Eall%7Esobaiduweb%7Edefault-0-130956719.142%5Ev100%5Econtrol; c_page_id=default; log_Id_pv=527; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1713077532; log_Id_view=13501; dc_tos=sbx73w'# }# content = ["非常不错的技术领域文章分享,解决了我在实践中的大问题!博主很有耐心,更有对知识的热忱和热爱,写了这么实用有效的分享,值得收藏点赞。"] # 评论内容# data = "commentId=&content=%s&articleId=%s" % (random.choice(content), articleId)# response = requests.post("https://blog.csdn.net/phoenix/web/v1/comment/submit",#                          data=data.encode('utf-8'),#                          headers=user_headers)# print(response.json())# if response.json()["message"] == "success":#     update_article(articleId)# if response.json()["code"] == 400:#     update_article(articleId)#time.sleep(random.randint(1, 5))  # 随机等待时间,减少机器行为特征comments = ["大佬互关吗?非常不错的文章,解决了我大问题!赞一个!","感谢分享,学到了很多新东西,期待更多的好文章!想和您互关一下。","文章写得很详细,内容丰富,对我帮助很大,感谢作者!加个关注呗,会持续关注你的文章!",]user_headers = {'Host': 'blog.csdn.net','Connection': 'keep-alive','Content-Length': '75','sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"','Accept': 'application/json, text/javascript, */*; q=0.01','X-Requested-With': 'XMLHttpRequest','sec-ch-ua-mobile': '?0','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','Origin': 'https://blog.csdn.net','Sec-Fetch-Site': 'same-origin','Sec-Fetch-Mode': 'cors','Sec-Fetch-Dest': 'empty','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9','Cookie': 'pluginId=kfkdboecolemdjodhmhmcibjocfopejo; pluginVersion=3.0.6; pluginUUID=10_87822374348-1701002105453-558370; uuid_tt_dd=10_6139708430-1714951961495-769741; c_adb=1; loginbox_strategy=%7B%22taskId%22%3A349%2C%22abCheckTime%22%3A1714951959215%2C%22version%22%3A%22exp11%22%7D; hide_login=1; UserName=m0_74120525; UserInfo=043691f18ed5485a89416662f3302d1b; UserToken=043691f18ed5485a89416662f3302d1b; UserNick=Byyyi%E8%80%80; AU=952; UN=m0_74120525; BT=1714951977580; p_uid=U010000; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22m0_74120525%22%2C%22scope%22%3A1%7D%7D; creative_btn_mp=3; firstDie=1; dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDUwODQzNywiZXhwIjoxNzE1NTU4MTI5LCJpYXQiOjE3MTQ5NTMzMjksInVzZXJuYW1lIjoibTBfNzQxMjA1MjUifQ.wSCwf0G8gvWT8KgwBGiG_-mDVPAy4wAzb40KHOQPJLI; _clck=3n9688%7C2%7Cflj%7C0%7C1586; historyList-new=%5B%5D; fe_request_id=1714959775761_3129_1139351; c_utm_source=yh-grzx; Hm_lvt_e5ef47b9f471504959267fd614d579cd=1714960468; ssxmod_itna=Gq+xuQG=DQoiqY5GHt+mDUheOt13xT4RB4GN4G8mDnqD=GFDK40oo7vhPoDOFBT67DKTxdrGvtTSFG4+NTi070Wu1tmDB3DEx0=qlUmi4GGAxBYDQxAYDGDDPRD84DrD7EZCS6xYPG0DGQD3qGySS=DAbtDbE6uDitVDDtH94G2D7tBFDDlFBuPYYxe8mU6zWidxGtqAwD48qDM0eGXbh9P8TR8X1xlALcHHnmuYmiDtqD9ej=Dbfddx0PBnk2zC0ez08vmmYPWQGGxG0hPQBxoQYnWzcwPID+sG7zQKu+1U5DAzb4lPD===; ssxmod_itna2=Gq+xuQG=DQoiqY5GHt+mDUheOt13xT4RDA6b5G8DBkq3P7pWQ7caY74DOGcHUw6oKK16lWuDx7q/ETor7YFOY5aqo8oTKhadGpY5IUgCYu07fjlFI0DngB4APpWoGck1g3yHQz177BFClQ7NVBcCd6bh/xy+3aB10WYeNtEbaR7OO4oNOb7i+EoGdbaCGo8O2hwGfbiQO49CnYfje0S+BQAHOt0pBzSpnYBtTfwXOSfH4bAXQNgIwPHQeNojFt6+Y4IkYzG+oKy0bwTpY+Mg07/plnZmw5zgmqzuGMSc2g8MdSfb48dIFe=ITOemXIwVeYMWv4rYB9G2GvEYiDu9C9H/z5BQPR3cpFI5qIGBYqOmuBymB9++2j3EftOw+qQoOIYOLIeQaP7bKtxv6WEPoDXigf9Bpb7K17I7oe5g4PeTwF0IqaUEqoKhnx792+jaO7hBUWaMSraHoD07VDDLxD2FGDD=; tfstk=f9_S9i_cSYD7CafXK3F2lTVEU3YCdTaaVXOds63r9ULJAW6AIHQyUJjCct6s8LCyJtkvsBOppQKKkbbfC2uzEufKRe8LQRza7_fy-eea_dPH3LAhMped2_hhZeYL3ztikQflCQHKZuTLGEdBT0Lp9QFjHC9xyB3J9xFXtKppJLppkmde1b3p93hKYxO0VK57LzxG4pNvU_pj5sbXwwnlwKgIRZsXVd3Bh23dlQ1DDwCq5kJOjL-wntUqPe1vOOO1FxeXJs_l5nQ8n261hGLXii2IAdsdaGjpc7URGU9WfZ-qwDT5vTbBqgG3ssLRnGvM2o2DGa7wAdxjH8CVGLK1ATymJLS1MttNuxuwk1IXlgyE7dG7rDGXj2OXQSNjxD-f0vXquGHZJ3dDgFPbGcoH2IAXQSNjxDxJiIJTGSiZx; dc_session_id=10_1714985059056.892798; dc_sid=55f3018b6d8e5aa88af744443a91103b; c_segment=13; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1714802801,1714951959,1714985542,1714985675; m0_74120525comment_new=1714969698928; c_first_ref=default; c_first_page=https%3A//blog.csdn.net/FontThrone/article/details/75136885; c_dsid=11_1714986736595.972711; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1714986986; c_pref=https%3A//blog.csdn.net/m0_74120525%3Ftype%3Dblog; c_ref=https%3A//blog.csdn.net/m0_74120525/article/details/137978562%3Fspm%3D1001.2014.3001.5502; c_page_id=default; log_Id_pv=305; log_Id_view=8143; log_Id_click=273; waf_captcha_marker=d3c5cc50cde89dc6410b96bdbd780118303fb132ef46b26967a50ce16c8d1ae3; dc_tos=sd25da'  # 请替换成你的cookie信息}content = random.choice(comments)  # 随机选择一条评论data = "commentId=&content=%s&articleId=%s" % (content, articleId)response = requests.post("https://blog.csdn.net/phoenix/web/v1/comment/submit",data=data.encode('utf-8'), headers=user_headers)response_json = response.json()print(response_json)if response_json.get("message") == "success":update_article(articleId)elif response_json.get("message") == "您评论太快了,请休息一下!":print("评论过快,正在休息...")time.sleep(60)  # 休息一分钟comment(articleId)  # 重新尝试评论print("===【评论功能】运行结束 ===\n")def qzzhrb():"""全站综合热榜"""print("===【获取全站综合热榜文章】运行开始 ===\n")headers1 = {'Host': 'blog.csdn.net','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36',}for y in range(0, 4):time.sleep(5)response = requests.get("https://blog.csdn.net/phoenix/web/blog/hotRank?page=" + str(y) + "&pageSize=25",headers=headers1)if response.json()["message"] == "success":for i in response.json()["data"]:for j in range(1, len(i["articleDetailUrl"])):if i["articleDetailUrl"][-j] == "/":articleId = i["articleDetailUrl"][-j + 1:]if select_is_insert(articleId) != 1:insert_article(articleId, i["articleDetailUrl"], i["articleTitle"], i["nickName"],i["hotRankScore"])breakbreakprint("===【获取全站综合热榜文章】运行结束 ===\n")def lynrb():print("===【获取领域内容榜文章】运行开始 ===\n")"""领域内容榜"""headers1 = {'Host': 'blog.csdn.net','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36',}list = ["python", "java", "javascript", "人工智能", "php", "c%2Fc%2B%2B", "大数据", "移动开发", "数据结构与算法","游戏", "网络", "运维", "测试"]for y in range(0, 2):for i in list:response = requests.get("https://blog.csdn.net/phoenix/web/blog/hotRank?page=" + str(y) + "&pageSize=25&child_channel=" + i,headers=headers1)time.sleep(5)if response.json()["message"] == "success":for i in response.json()["data"]:for j in range(1, len(i["articleDetailUrl"])):if i["articleDetailUrl"][-j] == "/":articleId = i["articleDetailUrl"][-j + 1:]if select_is_insert(articleId) != 1:insert_article(articleId, i["articleDetailUrl"], i["articleTitle"], i["nickName"],i["hotRankScore"])breakbreakprint("===【获取领域内容榜文章】运行结束 ===\n")def xjzzb():"""新晋作者榜"""print("===【获取新晋作者榜文章】运行开始 ===\n")headers1 = {'Host': 'blog.csdn.net','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36',}for y in range(0, 5):time.sleep(5)response = requests.get("https://blog.csdn.net/phoenix/web/blog/newUserRank?page=" + str(y) + "&pageSize=20",headers=headers1)if response.json()["message"] == "success":for i in response.json()["data"]:for j in range(1, len(i["articleDetailUrl"])):if i["articleDetailUrl"][-j] == "/":articleId = i["articleDetailUrl"][-j + 1:]if select_is_insert(articleId) != 1:insert_article(articleId, i["articleDetailUrl"], i["articleTitle"], i["nickName"],i["hotRankScore"])breakbreakprint("===【获取新晋作者榜文章】运行结束 ===\n")def recommend():"""推荐栏目"""print("===【获取推荐栏目文章】运行开始 ===\n")header = {"path": "/api/articles?type=more&category=home&shown_offset=0","accept-language": "zh-CN,zh;q=0.9","referer": "https://blog.csdn.net/","accept": "application/json, text/javascript, */*; q=0.01","X - Tingyun - Id": "im - pGljNfnc;r = 332305116","Sec - Fetch - Site": "same - origin","Sec - Fetch - Mode": "cors","Sec - Fetch - Dest": "empty","Accept - Encoding": "gzip, deflate, br","Accept - Language": "zh - CN, zh;q = 0.9","Host": "blog.csdn.net","Connection": "keep - alive","sec - ch - ua": '" Not A;Brand";v = "99", "Chromium";v = "90", "Google Chrome";v = "90"',"Accept": "application / json, text / javascript, * / *; q = 0.01","X - Requested - With": "XMLHttpRequest","sec - ch - ua - mobile": "?0","Cookie": "uuid_tt_dd=10_30743904980-1618379395370-717724; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%7D; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_30743904980-1618379395370-717724; __gads=ID=e49a2afa774ef751-22e5578266c70052:T=1618379398:RT=1618379398:S=ALNI_MbGqjNddCmz_vAd5NE9aUuroCHdwA; ssxmod_itna=YqGxcCD=0QK7qYKGHEoQ40OxUxmufqLLdr80i44GNYWDZDiqAPGhDCbbtxw0mBmDI=fijYav4j4biaPIoKQmOXxbDCPGnDB9+fpDem=D5xGoDPxDeDADYo6DAqiOD7T=DEDm48DaxDoDehI7DY5DhxDC0GPDwx0CAg04eG9s7=7Cd/94VxxeAxG1=40HKYSm5t8EeGv3+x0kU40OuP58U6YDU7b4fQioWhhedndeklTYlelK/SD46nuKiD+xoehrTnq9DDpXbm6DD===; ssxmod_itna2=YqGxcCD=0QK7qYKGHEoQ40OxUxmufqLLdr80DA=nxAeD/FCbDFx48kIZp7KAphO1Cx25eGozrjvh8kC2L5tYKCxjy+INZxC0+h/0tZMc8qC2CO=5aZgAKKHnShO94Y8V=uoW+9KyHY/zkQG8xX4HqLlUDNjK0QxOW0x9EYc0wYQixG2Y0PDFqD2YiD==; dc_session_id=10_1620356271093.957181; TY_SESSION_ID=0fae6181-358c-443f-9ab9-19d622159650; dc_sid=dcd3c87e76fb26c5661211133000d30c; c_first_ref=default; c_first_page=https%3A//blog.csdn.net/; c_segment=12; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1618379397,1619322330,1620356273; c_ref=https%3A//blog.csdn.net/; firstDie=1; log_Id_view=51; c_pref=https%3A//blog.csdn.net/; c_page_id=default; dc_tos=qspx78; log_Id_pv=10; c-login-auto=9; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1620359253; announcement-new=%7B%22isLogin%22%3Afalse%2C%22announcementUrl%22%3A%22https%3A%2F%2Fblog.csdn.net%2Fblogdevteam%2Farticle%2Fdetails%2F112280974%3Futm_source%3Dgonggao_0107%22%2C%22announcementCount%22%3A0%7D","user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"}list = ["python", "home", "career", "java", "web", "arch", "blockchain", "db", "5g", "game", "mobile", "ops", "sec","engineering"]for i in list:response = requests.get("https://blog.csdn.net/api/articles?type=more&category=%s&shown_offset=0" % i,headers=header)if response.json()["status"] == "true":for i in response.json()["articles"]:if select_is_insert(i["product_id"]) != 1:insert_article(i["product_id"], i["url"], i["title"], i["nickname"], i["views"])print("===【获取推荐栏目文章】运行结束 ===\n")def comment_page(id, page=1, size=18):"""查询是否已评论"""print("===【查询是否已评论】运行开始 ===\n")header = {'Host': 'blog.csdn.net','Connection': 'keep-alive','Content-Length': '0','sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"','Accept': '*/*','X-Requested-With': 'XMLHttpRequest','sec-ch-ua-mobile': '?0','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36','X-Tingyun-Id': 'im-pGljNfnc;r=301378265','Origin': 'https://blog.csdn.net','Sec-Fetch-Site': 'same-origin','Sec-Fetch-Mode': 'cors','Sec-Fetch-Dest': 'empty','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'zh-CN,zh;q=0.9','Cookie': 'uuid_tt_dd=10_30743904990-1609307614140-892319; UN=qq_39046854; p_uid=U010000; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_30743904990-1609307614140-892319!5744*1*qq_39046854; Hm_lvt_e5ef47b9f471504959267fd614d579cd=1614845242; __gads=ID=e3eda8954669b04c-22bf2c9088c700af:T=1619152098:RT=1619152098:S=ALNI_Mabu5XI7BhNuTCo5hOhZVC6RKaXJw; ssxmod_itna=Qqmx0Qi=D=0=nDeq0LP=4jo4RE7n7oiT+TrDlPpQxA5D8D6DQeGTT0dDB7Q1im7YHwTD578uhWx=m4ou=79C+oXPTDCPGnDBFh3TDee=D5xGoDPxDeDADYE6DAqiOD7qDdEsNv/8DbxYpnDA3Di4D+bkQDmqG0DDtHR4G2D7Un07Dqbu0jWWtohkDqwY+nD0t3xBLebaT5apaq0uiroPK48DDHtYyYQ0GCuxwCYBXTKqGyiKGuATUl9bRCOTXS/LvPthDLBhGCI7D6wAxqY7wC9gM5FYeqWhvQnGWnqReME0DDG8h=S2exD=; ssxmod_itna2=Qqmx0Qi=D=0=nDeq0LP=4jo4RE7n7oiT+D6aKp0DGqDsrdeeDLD=vR9bk=yn8Qk0IkUDnbmYyQjB9+FP6hOP2vebQd20Wa5U70=s5vbAxcwHwWzkr6nbVC3b0N2CdOIvRgQVByY2qdw874XwYWLDV+GM0AfmKDAyG+EPYQjxIRhk8jAx3kDV4MGkidjRfL0mD8Pvt8KGGYXOCPNGA=+sIP6K6P0CdRytv=4yl2FcBaH/bRc=4CFyBmK8=+OKppF4/=EvEcBhvu9iVcXmdRRsd/HW=sCLvk8yjfeDKdwiC3+Y=YhS4hrlZ+M0NVpGM7PDjKDeuD4D; UserName=qq_39046854; UserInfo=354c160126b1426e8ee609598d4e473f; UserToken=354c160126b1426e8ee609598d4e473f; UserNick=%E5%A4%A7%E5%AE%B6%E4%B8%80%E8%B5%B7%E5%AD%A6%E7%BC%96%E7%A8%8B%EF%BC%88python%EF%BC%89; AU=1D9; BT=1619320665305; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22qq_39046854%22%2C%22scope%22%3A1%7D%7D; c_first_ref=www.baidu.com; c_segment=15; dc_sid=3287252e7d613c9ce0fc0f7bff30cc8d; firstDie=1; c_first_page=https%3A//download.csdn.net/download/binzainet/11432043; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1620921821,1620924825,1621223521,1621223546; aliyun_webUmidToken=T2gA_B9FwF6d3YpegJJLr7sTunCxnLaUsnXsbtNlO-dk6-hgLZhENFiE0dyY2OFIiK8=; dc_session_id=10_1621301343026.272834; TY_SESSION_ID=5adae148-659f-4a3c-8354-3c27eafb9e82; announcement-new=%7B%22isLogin%22%3Atrue%2C%22announcementUrl%22%3A%22https%3A%2F%2Fblog.csdn.net%2Fblogdevteam%2Farticle%2Fdetails%2F112280974%3Futm_source%3Dgonggao_0107%22%2C%22announcementCount%22%3A0%2C%22announcementExpire%22%3A3600000%7D; log_Id_click=1149; c_ref=https%3A//blog.csdn.net/; log_Id_view=3430; c_pref=https%3A//blog.csdn.net/; c_utm_medium=distribute.pc_feed.none-task-blog-yuanlijihua_tag_v1-2.nonecase; c_page_id=default; dc_tos=qta45d; log_Id_pv=2040; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1621301378',}data = "page=%s&size=%s&commentId=" % (page, size)response = requests.post("https://blog.csdn.net/phoenix/web/v1/comment/list/%s?page=%s&size=%s&commentId=" % (id, page, size),headers=header, data=data)if response.json()["code"] == 200:list = response.json()["data"]["list"]if response.json()["data"]["count"] < size:for i in list:if i["info"]["userName"] == "qq_39046854":update_article(id)return Trueif response.json()["data"]["count"] == size:for i in list:if i["info"]["userName"] == "qq_39046854":update_article(id)return Trueif response.json()["data"]["count"] > size:for i in list:if i["info"]["userName"] == "qq_39046854":update_article(id)return Truecomment_page(id, 1, response.json()["data"]["count"])return Falseprint("===【查询是否已评论】运行结束 ===\n")# 每四分钟一次
def is_comment():not_comment = select_is_comment()if not_comment != ():for i in not_comment:if str(i[1][22:33]) in "qq_39046854weixin_43673589":update_article(i[0])else:y = comment_page(i[0])if y == False:comment(i[0])@repeat(every(10).minutes, func=recommend)
@repeat(every(2).minutes, func=is_comment)
# @repeat(every(3).minutes, func=is_comment)
@repeat(every().day.at("10:00"), func=qzzhrb)
@repeat(every().day.at("09:00"), func=lynrb)
@repeat(every().day.at("07:00"), func=xjzzb)
def run_threaded(func):job_thread = threading.Thread(target=func)job_thread.start()def main():while True:try:run_pending()  # run_pending:运行所有可以运行的任务except:passtime.sleep(1)if __name__ == '__main__':main()

如何对代码进行自定义修改,纳为己用?

自定义需要修改的部分:

1.数据库连接配置
POOL = PooledDB(creator=pymysql,host='127.0.0.1',  # 数据库主机地址port=3306,  # 数据库端口号user='xxx',  # 数据库用户名password='xxx',  # 数据库密码database='csdn_article',  # 数据库名称charset='utf8'
)

需要根据自己的数据库配置,修改 host、port、user、password 和 database。

2.插入文章函数
sql = "INSERT INTO `article` (`articleId`, `articleDetailUrl`, `articleTitle`, `nickName`, `hotRankScore`) VALUES (%s, %s, %s, %s, %s);"
data = (articleId, articleDetailUrl, articleTitle, nickName, hotRankScore)

用户需要确保数据库中有名为 article 的表,并且表结构与上述 SQL 语句匹配。

3.Cookie信息
user_headers = {'Cookie': '你的cookie信息'  # 替换为实际的Cookie
}

用户需要替换 user_headers 中的 Cookie 值为实际的 Cookie 信息。

4.评论内容

此处将评论内容设置为一个数组,随机输出数组内的一条评论。

comments = ["大佬互关吗?非常不错的文章,解决了我大问题!赞一个!","感谢分享,学到了很多新东西,期待更多的好文章!想和您互关一下。","文章写得很详细,内容丰富,对我帮助很大,感谢作者!加个关注呗,会持续关注你的文章!",
]
5.定时任务
@repeat(every(10).minutes, func=recommend)
@repeat(every(2).minutes, func=is_comment)
@repeat(every().day.at("10:00"), func=qzzhrb)
@repeat(every().day.at("09:00"), func=lynrb)
@repeat(every().day.at("07:00"), func=xjzzb)

根据自己的需求调整任务的执行频率和时间。

安装依赖

标准库
import random
import time
import os
import threading
第三方库
import requests
import pymysql
from dbutils.pooled_db import PooledDB
from schedule import every, repeat, run_pending

需要确保安装这些第三方库,可以使用以下命令安装:

pip install requests pymysql dbutils schedule

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/15514.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ClickHouse 24.4 版本发布说明

本文字数&#xff1a;13148&#xff1b;估计阅读时间&#xff1a;33 分钟 审校&#xff1a;庄晓东&#xff08;魏庄&#xff09; 本文在公众号【ClickHouseInc】首发 新的一个月意味着新版本的发布&#xff01; 发布概要 本次ClickHouse 24.4版本包含了13个新功能&#x1f381;…

ML307R OpenCPU DEMO_SDK环境搭建

一、工程目录 二、环境搭建 三、编译 四、下载 五、添加自定义文件打印 hello 一、工程目录 OpenCPU SDK代码目录结构&#xff0c;如下图所示&#xff1a; | 名称 | 描述 | | ---------------- | --------------------------| | custom …

软件设计师中级

计算机系统 运算器和控制器 算术逻辑单元 累加寄存器器 状态寄存器 数据缓冲寄存器 指令寄存器 程序计数器 地址寄存器 指令译码器 内存按字节编址 内存存储单元16位 1 浮点数 浮点数范围&#xff1a;-2的(2的阶码次)-1到-2的(2的阶码次)-1 乘 1-2负尾数次 海明码 海明码&…

自定义一个SpringBoot场景启动器

前言 一个刚刚看完SpringBoot自动装配原理的萌新依据自己的理解写下的文章&#xff0c;如有大神发现错误&#xff0c;敬请斧正&#xff0c;不胜感激。 分析SpringBoot自动配置原理 SpringBoot的启动从被SpringBootApplication修饰的启动类开始,SpringBootApplicaiotn注解中最…

C语言 宏

目录 一、宏定义 1.1 预定义符号 1.2 预处理指令 #define 1.3 带有副作用宏定义 1.4 宏和函数的一个对比 ​编辑 1.5 #undef 二、条件编译 2.1 #if、#else、#elif、#endif 2.2 #ifdef和#ifndef 2.3 C语言中如何通过条件编译来预防头文件的重复包含&#xff1f; 一、宏定义 在C语…

导弹初识(一)

目录 导弹初识1 导弹是什么2 导弹的分类2.1 按飞行方式2.2 按发射/目标2.2.1 空空导弹2.2.1 空地导弹2.2.1 地空导弹2.2.1 地地导弹 2.3 按打击目标 3.实例3.1 防空导弹3.2 低空防空导弹武器系统 本文节选自 zh&#xff0c;还有百度百科 导弹初识 1 导弹是什么 导弹两个字拆…

欧拉函数、快速幂、扩展欧几里得算法、中国剩余定理和高斯消元

欧拉函数 给定 n 个正整数 ai&#xff0c;请你求出每个数的欧拉函数。 欧拉函数的定义1∼N 中与 N 互质的数的个数被称为欧拉函数&#xff0c;记为 ϕ(N)。 若在算数基本定理中&#xff0c;Np1a11p2a2…pmm&#xff0c;则&#xff1a;ϕ(N) Np1−1/p1p2−1/p2…pm−1/pm 输…

二叉树基于队列实现的操作详解

一、队列知识补充 有关队列的知识请详见博主的另一篇博客&#xff1a;http://t.csdnimg.cn/3PwO4 本文仅仅附上需要的队列操作供读者参考 //结构体定义 typedef struct BinaryTreeNode* QDataType;typedef struct QueueNode {struct QueueNode* next;QDataType val; }QNode;…

添砖Java(十一)——常见类的使用Object,Math,System,BigDeciaml,包装类

目录 object&#xff1a; toString&#xff1a; equals: ​编辑 Math&#xff1a;​编辑 System: BigDecimal: 基本数据的包装类&#xff1a;​编辑 object&#xff1a; 我们知道&#xff0c;所有的类都是间接或直接继承了object类。然后object里面有几个用得很多的方法…

7.2k star的万能视频解析下载插件

今天给大家介绍一个超级厉害的浏览器插件&#xff0c;可以解析各个平台网页视频——猫抓。 项目简介 猫抓&#xff08;cat-catch&#xff09; 是一款资源嗅探扩展插件&#xff0c;他能够帮助你筛选列出当前页面的资源。简单来说&#xff0c;当你打开任意一个带有视频的网页&a…

信息系统项目管理师十大管理计划内容概览

目录 1.项目章程2.项目管理计划3.范围管理计划4.需求管理计划5.进度管理计划6.成本管理计划7.质量管理计划8.资源管理计划9.沟通管理计划10.风险管理计划11.采购管理计划12.干系人参与计划 点我去AIGIS公众号查看本文 1.项目章程 项目目标成功标准退出标准关键干系人名单发起人…

Android9.0 MTK平台如何增加一个系统应用

在安卓定制化开发过程中&#xff0c;难免遇到要把自己的app预置到系统中&#xff0c;作为系统应用使用&#xff0c;其实方法有很多&#xff0c;过程很简单&#xff0c;今天分享一下我是怎么做的&#xff0c;共总分两步&#xff1a; 第一步&#xff1a;要找到当前系统应用apk存…

Linux_应用篇(08) 信号-基础

本章将讨论信号&#xff0c;虽然信号的基本概念比较简单&#xff0c;但是其所涉及到的细节内容比较多&#xff0c;所以本章篇幅也会相对比较长。 事实上&#xff0c;在很多应用程序当中&#xff0c;都会存在处理异步事件这种需求&#xff0c;而信号提供了一种处理异步事件的方法…

c# 画一个正弦函数

1.概要 c# 画一个正弦函数 2.代码 using System; using System.Drawing; using System.Windows.Forms;public class SineWaveForm : Form {private const int Width 800;private const int Height 600;private const double Amplitude 100.0;private const double Period…

lvgl无法显示中文

环境&#xff1a; VS2019、LVGL8.3 问题&#xff1a; VS2019默认编码为GB2312&#xff0c; 解决&#xff1a; VS2022设置编码方式为utf-8的三种方式_vs utf8-CSDN博客 我用的方法2&#xff0c;设置为 utf-8无签名就行。

深入了解Socket套接字

目录 一、引入&#x1f64c; 1、概念 &#x1f389; 2、分类&#x1f389; Socket 套接字主要针对传输层协议分为流套接字、数据报套接字、原始套接字&#xff08;了解即可&#xff09;三类。 1&#xff09;流套接字&#xff1a;使用传输层TCP协议 2&#xff09;数据报套…

AI预测福彩3D采取888=3策略+和值012路一缩定乾坤测试5月25日预测第1弹

上一套算法采用了88723的容差策略&#xff0c;关于容差策略相信大家都比较清楚&#xff1a;容差可以最大限度的保证初始大底中包含中奖号码&#xff0c;然后再通过设置一些杀号条件进行缩水。比如&#xff0c;我对我的各种模型算法近30期的预测结果进行了统计&#xff0c;如果采…

huggingface笔记: accelerate estimate-memory 命令

探索可用于某一机器的潜在模型时&#xff0c;了解模型的大小以及它是否适合当前显卡的内存是一个非常复杂的问题。为了缓解这个问题&#xff0c;Accelerate 提供了一个 命令行命令 accelerate estimate-memory。 accelerate estimate-memory {MODEL_NAME} --library_name {LIBR…

Stable Diffusion【艺术特效】【霓虹灯】:霓虹灯像素化马赛克特效

提示词 Neon pixelated mosaic of [Subject Description],highly detailed [主题]的霓虹灯像素化马赛克&#xff0c;高度详细 参数设置 大模型&#xff1a;万享XL_超写实摄影V8.2 采样器&#xff1a;Euler a 采样迭代步数&#xff1a;25 CFG&#xff1a;3 反向提示词&#x…

微服务可用性之隔离

摘要 ​ 本文主要微服务场景下服务的可用性保障之隔离。隔离又分为几种情况&#xff0c;动静隔离、读写隔离、热点隔离、资源隔离等场景。 为什么要隔离 ​ 本质上是对资源进行分割确保在出现故障的时候服务只是部分不可用&#xff0c;不至于系统陷入整体性瘫痪&#xff0c;…