声明
该脚本的目的只是为了提升博客创作效率和博主互动效率,希望大家还是要尊重各位博主的劳动成果。
数据库设计
尽量我们要新建一个数据库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