树莓派俱乐部 树莓派俱乐部
  • 首页
  • 树莓派
  • OpenWrt
  • Python
  • Linux
  • 注册
    登录
现在登录。
  • 首页
  • 树莓派
  • OpenWrt
  • Python
  • Linux
首页 Python Python3下WordPress 数据库发布文章的类

Python3下WordPress 数据库发布文章的类

束发少年 4年 前

文章前言

前段时间,这么说应该是去年,想利用树莓派做个爬虫服务器,然后发布到WordPress上,搜索找到eqblog博客上有个Python3下WordPress 数据库发布文章的类,所以试了一下非常不错,做个记录,有需要的可以试试看。

这个代码只使用在Python3上,在使用之前需要安装PyMsql库:

pip3 install PyMySQL

代码部分

class wordpress_post:
    def __init__(self,tittle,content):
        self.tittle=tittle
        self.content=content
    def mysql_con(self):
        conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='pwd', db='wordpress', charset='utf8') #将这里换为你的数据库地址
        return conn
    def up(self):
        times=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        sql="INSERT INTO wp_posts(post_author,post_date,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_name,to_ping,pinged,post_modified,post_content_filtered,post_parent,menu_order,post_type,comment_count) VALUES ('1','%s','%s','%s','','publish','open','open','%s','','','%s','','0','0','post','0')" % (str(times),str(self.content),str(self.tittle),str(self.tittle),str(times))
        return sql
    def cat(self,ids,cat):
        sql="INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (%s,%s,'0')"%(ids,cat)
        return sql
    def close_mysql(self,cursor,conn):
        conn.commit()
        cursor.close()
        conn.close()
[tip type="tip success" ]

记得上面代码的conn变量中设置你的数据库地址和用户名密码

[/tip]
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='pwd', db='wordpress', charset='utf8') #将这里换为你的数据库地址

使用方法

 a=wordpress_post(str(t),wz_content) #这里第一个参数是标题 第二个是文章内容
 conn=a.mysql_con()
 cursor = conn.cursor()
 c=a.up()
 effect_row = cursor.execute(c)
 new_id = cursor.lastrowid #这里是记录文章id以便设置文章的分类
 d=a.cat(new_id,'1')
 effect_row = cursor.execute(d)
 a.close_mysql(cursor,conn)

其实自己可以结合上面的代码利用多线程做个爬虫是非常方便的。

#Python#爬虫
0
束发少年
用自己的眼睛去读世间这一部书。
评论 (1)
返回
  • 优雅的树莓

    不错不错,可以拿来做爬虫了。

    4年 前 回复
猜你喜欢
  • Python 实现多线程分块下载
  • 用Python统计大文件内容行数
束发少年
28
文章
7
评论
86
获赞
文章聚合
如何在树莓派安装Docker
4年 前
用树莓派实现Xbox one连接自建Minecraft服务器
3年 前
树莓派Ubuntu 19.04安装PHP7.3
4年 前
如何构建树莓派64位系统(1)构建根目录
3年 前
如何在 Raspberry Pi 上安装 Apache 和 PHP
3年 前
  • 首页
Copyright © 2018-2023 树莓派俱乐部. Designed by nicetheme.