wordpress文章采集软件(把网站上的所有文章全部导出到本地(组图) )

优采云 发布时间: 2021-12-09 00:10

  wordpress文章采集软件(把网站上的所有文章全部导出到本地(组图)

)

  做程序员的四年里,写了400多篇博客和笔记,全部发布在我的个人网站上,很多都没有在本地存档。

  今天突发奇想,想把网站上的文章全部导出到本地,方便本地参考和内容更新。

  但是环顾四周,目前还没有专门的插件可以做到这一点。最后也没有办法,只能自己研究。

  整理任务,拆解:

  Step 1:理清数据库表之间的关系 Step 2:将csv数据导出到本地 Step 3:编写脚本,将csv数据的内容分类生成md文件1.数据库表

  首先明确我们的目标是将文章的原创md保存在本地,这就决定了我们需要这些数据:

  文章 的标题和内容可以在 wordpress.wp_posts 表中轻松找到。字段名称是:

  最麻烦的是文章的分类。

  我环顾四周,发现 wordpress 如何管理这些 文章。

  原来wp_term_relationships表中记录了每个文章的term_taxonomy_id,一个文章中可能有多个term_taxonomy

  

  为什么有多个term_taxonomy?如何理解这个term_taxonomy?

  分类法的中文解释是分类的意思。

  wordpress中term_taxonomy有很多种,包括类别(category)、标签(tag)和导航菜单(nav_menu)

  它们使用的同一个表中的记录存储在 wp_term_taxonomy 表中。

  

  数据库表理顺后,写SQL语句的逻辑就清晰了

  use wordpress;

select

p.post_title,t.name,p.post_content_filtered

from wp_posts p, wp_term_relationships r,wp_terms t, wp_term_taxonomy tt

where p.id=r.object_id

and r.term_taxonomy_id=t.term_id

and tt.term_id=t.term_id

and tt.taxonomy='category';

  2. 导出csv数据

  MySQL导出csv数据,可以使用into语法,但是这个语法需要设置--secure-file-priv,所以觉得麻烦就不用了。

  于是打开我的数据库连接管理软件(DBeaver),输入上面的SQL语句,直接将查询结果导出到本地的posts.csv文件

  

  3. 将数据整理成 md 文件

  post.csv 只有三个数据

  "post_title","name","post_content_filtered"

  编写一个 Python 脚本来处理它

  import os

import csv

WORK_DIR = os.getcwd()

def set_post_dir():

post_dir = os.path.join(WORK_DIR, category)

if not os.path.exists(post_dir):

os.mkdir(post_dir)

os.chdir(post_dir)

with open('posts.csv', newline='') as csvfile:

posts = csv.reader(csvfile)

for post in posts:

if post[1] == "name":

continue

post_title, category, post_content = post

set_post_dir()

try:

with open(post_title+".md", "w") as post_file:

post_file.write(post_content)

except Exception as exp:

print(exp)

print(f"category: {category}")

print(f"title: {post_title}")

print(f"content: {post_content}")

  运行后,在本地查看,每个类别都有一个目录

  

  并且每个分类都有自己的文章的markdown原文

  

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线