Python 采集每日一词到博客中
每天将词霸的每日一句采集到自己的博客中
接口地址及参数
例:http://open.iciba.com/dsapi?data='2022-01-01'
完整代码
导入的包自行处理一下,没有用到那么多。
from __future__ import print_function
import requests
import csv
from bs4 import BeautifulSoup
import os, sys
import time
import urllib3
import json
import arrow
import datetime
import pymysql
if __name__ == '__main__':
# 获取今天的日期
day= datetime.datetime.now().date()
dat={"date": day}
resp=requests.post("http://open.iciba.com/dsapi/",data=dat)
json_obj = json.loads(resp.content)
db = pymysql.connect(
host="*****",
port=3306,
user='***',
passwd='***',
db='****'
)
cur = db.cursor(cursor=pymysql.cursors.DictCursor)
text = str('<!--markdown-->>'+json_obj['content']+'\r\n>'+json_obj['note']+'\r\n\r\n\r\n![每日一词][1]\r\n\r\n\r\n [1]:' + json_obj['fenxiang_img']).strip()
print(type(text))
sql='''
INSERT INTO t_contents (
title,
slug,
created,
modified,
text,
`order`,
authorId,
template,
type,
`status`,
`password`,
commentsNum,
allowComment,
allowPing,
allowFeed,
parent
)
VALUES
(
"'''+str(json_obj['dateline']) +'''",
"'''+str(json_obj['dateline']) +'''",
'''+ str(time.time()) +''',
'''+ str(time.time()) +''',
"'''+text+'''",
0,
1,
'',
'post',
'publish',
'',
0,
1,
1,
1,
0
)
'''
cur.execute(sql)
#获取最后插入的文章id
lastid = str(cur.lastrowid)
#给文章添加分类,否则前端不显示,分类id自己去表中查看
sql1 = 'INSERT INTO t_relationships(`cid`,`mid`) VALUES('+ lastid +',1)'
cur.execute(sql1)
sql2 = 'INSERT INTO t_relationships(`cid`,`mid`) VALUES('+ lastid +',8)'
cur.execute(sql2)
sql3 = 'INSERT INTO t_relationships(`cid`,`mid`) VALUES('+ lastid +',9)'
cur.execute(sql3)
db.commit()
cur.close()
db.close()
加到任务计划中
15 08 * python3 /www/wwwroot/sh/caption.py