【Python】一段WordPress文章入库的代码。。
本帖最后由 小心boss 于 2020-3-19 09:09 编辑刚好自己要用,网上搜了下好像没有能正常用的。修修补补了一下。
亲测正常使用,人懒没办法,什么都想偷懒。。。。
class wordpress_post:
def __init__(self, tittle, content):
self.tittle = tittle
self.content = content
self.postname = parse.quote(str(tittle))
def mysql_con(self):
conn = pymysql.connect(host='www.123', port=3306, user='w123', passwd='EX123RyB',
db='w12312123, charset='utf8')# 在此修改数据库信息
return conn
def up(self):
times = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sql = "INSERT INTO tu_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), self.postname, str(times))
return sql
def cat(self, ids, cat):
sql = "INSERT INTO tu_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()
def post_article(info, tittle):
with open('temp/log.txt', 'a+') as f:
f.write(str(info) + 'n')
with open('temp/temp.txt', 'r') as f:
wz_content = f.read()
os.remove('temp/temp.txt')
a = wordpress_post(str(tittle), wz_content)
conn = a.mysql_con()
cursor = conn.cursor()
c = a.up()
effect_row = cursor.execute(c)
new_id = cursor.lastrowid
d = a.cat(new_id, '1')
effect_row = cursor.execute(d)
a.close_mysql(cursor, conn) yulinsoft 发表于 2020-3-19 09:04
没看到执行sql的语句呀,少 了吧!
啊哈~忘了 学了基础后面的就没时间去学了。 我用shell脚本也写了一个,看到大佬写的,突然感觉我的就不香了..{:1_908:} 不错,省的自己写了,收了感谢
页:
[1]