使用python自動釋出部落格園
阿新 • • 發佈:2021-11-17
依賴
import re
import xmlrpc.client as xmlrpclib
from random import random, randint
from time import strftime
from bs4 import BeautifulSoup
import requests
上傳部落格函式
def postBlog(BlogContent, title): #標籤 cate_list = ["[隨筆分類]上傳測試"] post = dict(dateCreate=strftime("%Y%m%dT%H:%M:%S"), description=BlogContent, title=title, categories=cate_list) server.metaWeblog.newPost(getBlogId(), usr, passwd, post, True)
BlogContent 部落格內容
title 部落格標題
categories 標籤分類
獲取部落格id
serviceUrl, appkey = 'http://rpc.cnblogs.com/metaweblog/L-L-ALICE', 'L-L-ALICE' usr, passwd = '賬號', '密碼' server = xmlrpclib.ServerProxy(serviceUrl) def getBlogId(): blogInfo = server.blogger.getUsersBlogs(appkey, usr, passwd) return blogInfo[0]['blogid']
我的完整程式碼
import re import xmlrpc.client as xmlrpclib from random import random, randint from time import strftime from bs4 import BeautifulSoup import requests #############FBI WARNING############# #############修改 此處即可############# serviceUrl, appkey = 'http://rpc.cnblogs.com/metaweblog/L-L-ALICE', 'L-L-ALICE' usr, passwd = '賬號', '密碼' #################################### server = xmlrpclib.ServerProxy(serviceUrl) # 在字串指定位置插入字元 # str_origin:源字串 pos:插入位置 str_add:待插入的字串 def str_insert(str_origin, pos, str_add): str_list = list(str_origin) # 字串轉list str_list.insert(pos, str_add) # 在指定位置插入字串 str_out = ''.join(str_list) # 空字元連線 return str_out def getContent(): url = 'https://www.runoob.com/' url2s = ['vue2', 'html', 'css', 'js', 'htmldom', 'jquery', 'python3'] for a in url2s: # 獲取原始碼 html = requests.get("https://www.runoob.com/" + a) # 列印原始碼 soup = BeautifulSoup(html.text, 'lxml') list = soup.find_all(href=re.compile(a)) urls = [] for x in list: urls.append(str_insert(re.sub('\t|\n', '', str(x)), 9, url)) return urls def getBlogId(): blogInfo = server.blogger.getUsersBlogs(appkey, usr, passwd) return blogInfo[0]['blogid'] def creatTitle(): title = strftime('%m') + '月' + strftime('%d') + '日' + '總結' return title def creatBlogContent(): # 這一天干了啥 Done # 打算幹啥 Will # 程式碼數 codeNum list = getContent() codeNum = randint(50, 500) donum = randint(0, len(list) - 1) return '今天干了啥:' + str(list[donum]) + '\n打算幹啥:' + str(list[donum + 1]) + '程式碼數:' + str(codeNum) def postBlog(BlogContent, title): #標籤 cate_list = ["[隨筆分類]上傳測試"] post = dict(dateCreate=strftime("%Y%m%dT%H:%M:%S"), description=BlogContent, title=title, categories=cate_list) server.metaWeblog.newPost(getBlogId(), usr, passwd, post, True) if __name__ == "__main__": postBlog(creatBlogContent(), creatTitle())