文章目录
- 自己最近搭建了一个网站【历史的今天】,数据都是通过爬虫去爬取数据然后整理而成,由于结构单一,就想到了用写一个代码来后台每天自动更新,减少自己的负担。刚开始是研究数据库的,准备直接从数据库下手,最后发现倒是能够生成数据但自己主题亦或是没找到其它数据库的关系,文章设定为固定链接是无法访问的会直接跳到404页面。后来发现有python实现就简单了。
-
- 自己在固定链接访问文章出问题了,但默认配置应该没有任何没有问题。有想尝试的可以去做参考!我只找到需要修改3个数据表,其它没发现要修改! 安装PYMSQL pip3 install PyMySQL 测试代码 我数据库是wp_开头的,自己注意按情况去修改! #!/usr/bin/python3 import pymysql # 打开数据库连接 db = pymysql.connect("localhost", "user", "password", "db_name") # 使用cursor()方法获取操作游标 cursor = db.cursor() #数据来源 post_content = 'hh' post_title = "hh" post_name = "hh" times=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) # SQL 语句 # 插入文章的操作 sql = """INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent,guid,menu_order,post_type,post_mime_type,comment_count) VALUES ('1','%s','%s','%s','%s','','publish','closed','closed','','%s','','','%s','%s','','0','https://history.emperinter.info/?p=43','0','post','','0')""" % (str(times),str(times),str(post_content),str(post_title),str(post_name),str(times),str(times)) #分类确定 sql2 = """ INSERT INTO wp_term_relationships(object_id,term_taxonomy_id) VALUES ((select ID from wp_posts where post_name = '%s'),1) """ % (str(post_name)) #wp_term_taxonomy更新; sql3 = """ update wp_term_taxonomy set count = (select count(ID) from wp_posts where post_type = "post" AND post_status = "publish") """ try: # 执行sql语句 cursor.execute(sql) # 提交到数据库执行 db.commit() print("sql") # 执行sql1语句 cursor.execute(sql2) # 提交到数据库执行 db.commit() print("sql2") # 执行sql2语句 cursor.execute(sql3) # 提交到数据库执行 db.commit() print("sql3") except: # 如果发生错误则回滚 db.rollback() # 关闭数据库连接 db.close()
- 使用的是python-wordpress-xmlrpc 测试自己网站是否开启了xmlrpc 输入网址:https://yoursite/xmlrpc.php,结果如图则没有任何问题 安装 pip3 install python-wordpress-xmlrpc 测试代码 #coding:utf-8 from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo from wordpress_xmlrpc.methods import posts from wordpress_xmlrpc.methods import taxonomies from wordpress_xmlrpc import WordPressTerm from wordpress_xmlrpc.compat import xmlrpc_client from wordpress_xmlrpc.methods import media, posts # 写入Wordpress wp = Client('https://your_site/xmlrpc.php', 'your_wordPress_name', 'your_wordPress_password') post = WordPressPost() post.title = 'post_title' post.content = 'post_content' post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布 post.terms_names = { 'post_tag': ['post_tag_0','post_tag_1'], #文章所属标签,没有则自动创建 'category': ['post_category'] #文章所属分类,没有则自动创建 } post.id = wp.call(posts.NewPost(post)) #方便写脚本生成日志,后期如有问题则好检查问题所在! # print(" %s : ok!" %today)
自己最近搭建了一个网站【历史的今天】,数据都是通过爬虫去爬取数据然后整理而成,由于结构单一,就想到了用写一个代码来后台每天自动更新,减少自己的负担。刚开始是研究数据库的,准备直接从数据库下手,最后发现倒是能够生成数据但自己主题亦或是没找到其它数据库的关系,文章设定为固定链接是无法访问的会直接跳到404页面。后来发现有python实现就简单了。
自己最近搭建了一个网站【历史的今天】,数据都是通过爬虫去爬取数据然后整理而成,由于结构单一,就想到了用写一个代码来后台每天自动更新,减少自己的负担。刚开始是研究数据库的,准备直接从数据库下手,最后发现倒是能够生成数据但自己主题亦或是没找到其它数据库的关系,文章设定为固定链接是无法访问的会直接跳到404页面。后来发现有python实现就简单了。
自己在固定链接访问文章出问题了,但默认配置应该没有任何没有问题。有想尝试的可以去做参考!我只找到需要修改3个数据表,其它没发现要修改!
- 安装PYMSQL
pip3 install PyMySQL
- 测试代码
我数据库是wp_开头的,自己注意按情况去修改!
#!/usr/bin/python3
import pymysql
# 打开数据库连接
db = pymysql.connect("localhost", "user", "password", "db_name")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
#数据来源
post_content = 'hh'
post_title = "hh"
post_name = "hh"
times=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
# SQL 语句
# 插入文章的操作
sql = """INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent,guid,menu_order,post_type,post_mime_type,comment_count)
VALUES ('1','%s','%s','%s','%s','','publish','closed','closed','','%s','','','%s','%s','','0','https://history.emperinter.info/?p=43','0','post','','0')""" % (str(times),str(times),str(post_content),str(post_title),str(post_name),str(times),str(times))
#分类确定
sql2 = """
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id)
VALUES ((select ID from wp_posts where post_name = '%s'),1)
""" % (str(post_name))
#wp_term_taxonomy更新;
sql3 = """
update wp_term_taxonomy set count = (select count(ID) from wp_posts where post_type = "post" AND post_status = "publish")
"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
print("sql")
# 执行sql1语句
cursor.execute(sql2)
# 提交到数据库执行
db.commit()
print("sql2")
# 执行sql2语句
cursor.execute(sql3)
# 提交到数据库执行
db.commit()
print("sql3")
except:
# 如果发生错误则回滚
db.rollback()
# 关闭数据库连接
db.close()
自己在固定链接访问文章出问题了,但默认配置应该没有任何没有问题。有想尝试的可以去做参考!我只找到需要修改3个数据表,其它没发现要修改!
pip3 install PyMySQL我数据库是wp_开头的,自己注意按情况去修改!
#!/usr/bin/python3
import pymysql
# 打开数据库连接
db = pymysql.connect("localhost", "user", "password", "db_name")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
#数据来源
post_content = 'hh'
post_title = "hh"
post_name = "hh"
times=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
# SQL 语句
# 插入文章的操作
sql = """INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent,guid,menu_order,post_type,post_mime_type,comment_count)
VALUES ('1','%s','%s','%s','%s','','publish','closed','closed','','%s','','','%s','%s','','0','https://history.emperinter.info/?p=43','0','post','','0')""" % (str(times),str(times),str(post_content),str(post_title),str(post_name),str(times),str(times))
#分类确定
sql2 = """
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id)
VALUES ((select ID from wp_posts where post_name = '%s'),1)
""" % (str(post_name))
#wp_term_taxonomy更新;
sql3 = """
update wp_term_taxonomy set count = (select count(ID) from wp_posts where post_type = "post" AND post_status = "publish")
"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
print("sql")
# 执行sql1语句
cursor.execute(sql2)
# 提交到数据库执行
db.commit()
print("sql2")
# 执行sql2语句
cursor.execute(sql3)
# 提交到数据库执行
db.commit()
print("sql3")
except:
# 如果发生错误则回滚
db.rollback()
# 关闭数据库连接
db.close()
使用的是python-wordpress-xmlrpc
- 测试自己网站是否开启了xmlrpc
输入网址:https://yoursite/xmlrpc.php,结果如图则没有任何问题

- 安装
pip3 install python-wordpress-xmlrpc
- 测试代码
#coding:utf-8
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods import taxonomies
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
# 写入Wordpress
wp = Client('https://your_site/xmlrpc.php', 'your_wordPress_name', 'your_wordPress_password')
post = WordPressPost()
post.title = 'post_title'
post.content = 'post_content'
post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布
post.terms_names = {
'post_tag': ['post_tag_0','post_tag_1'], #文章所属标签,没有则自动创建
'category': ['post_category'] #文章所属分类,没有则自动创建
}
post.id = wp.call(posts.NewPost(post))
#方便写脚本生成日志,后期如有问题则好检查问题所在!
# print(" %s : ok!" %today)
使用的是python-wordpress-xmlrpc
- 测试自己网站是否开启了xmlrpc
输入网址:https://yoursite/xmlrpc.php,结果如图则没有任何问题

pip3 install python-wordpress-xmlrpc#coding:utf-8
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods import taxonomies
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
# 写入Wordpress
wp = Client('https://your_site/xmlrpc.php', 'your_wordPress_name', 'your_wordPress_password')
post = WordPressPost()
post.title = 'post_title'
post.content = 'post_content'
post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布
post.terms_names = {
'post_tag': ['post_tag_0','post_tag_1'], #文章所属标签,没有则自动创建
'category': ['post_category'] #文章所属分类,没有则自动创建
}
post.id = wp.call(posts.NewPost(post))
#方便写脚本生成日志,后期如有问题则好检查问题所在!
# print(" %s : ok!" %today)
