最近写了一段代码,实现了采集文章后自动上传到博客的脚步,之前也想过要搞一个,当时思路是通过数据库,将采集的内容更新到数据库里。当然,这样是可以实现的。之后又有了一个思路,就是避开数据库,直接后台操作。搞了一段时间,一直找不到一个重要的参数,最近几天终于找到了,于是就有了以下的一段代码,现在分享出来:
import requests import json from hashlib import md5 from bs4 import BeautifulSoup from lxml import etree import re def denglu(): post_url='https://liaojinhua.com/zb_system/cmd.php?act=verify' session = requests.session() headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36' } s = md5() s.update(密码.encode()) daf=s.hexdigest() data = { # "btnPost": "登录", "username": 用户名, "password": s.hexdigest(), # "savedate": "1" } session.post(url=post_url, data=data, headers=headers) return session def shuru(title,biaoqian,zhengwen): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36' } get_url = 'https://liaojinhua.com/zb_system/admin/index.php?act=ArticleMng' session=denglu() html = session.get(url=get_url) ace = etree.HTML(html.text) wenzhang = ace.xpath('//div[@class="divHeader"]/text()') print(wenzhang) lianjian = ace.xpath('//div[@id="divMain2"]/table//tr[2]/td[@class="td10 tdCenter"]/a/@href') soup = BeautifulSoup(html.text, 'lxml') IDS = soup.find_all('td', class_='td5') print(IDS[0].text) ids = int(IDS[0].text) ids+=1 url = re.sub('Del&id=\d+', 'Pst', lianjian[1]) print(url) data = { # 设置文章的ID 'ID':ids, 'Type': '0', #设置标题 'Title': title, # 'Content': '<p>23232<br/></p>', # 'meta_jiage': None, # 'meta_shuoming': None, # 'meta_lianjie': None, # 'Alias': None, # 设置标签 'Tag': biaoqian, # 'Intro': 'None', # 设置文章上传分类 'CateID': '16', 'Status': '0', # 设置文章使用模板 'Template': 'single', # 作者ID 'AuthorID': '1', # 'PostTime': '2020-12-15 21:12:49',时间可以不设置 # 设置文章是否置顶 'IsTop': '0', 'IsLock': '0', # 摘要设置 # 'Intro':'None', # 内容设置 'Content': zhengwen} try: res = session.post(url, data=data, headers=headers) except Exception as f: print(f)
评论