用python写的一个wordpress的采集程序


本文整理自网络,侵删。

在学习python的过程中,经过不断的尝试及努力,终于完成了第一个像样的python程序,虽然还有很多需要优化的地方,但是目前基本上实现了我所要求的功能,先贴一下程序代码:

具体代码如下:

#! /usr/bin/python
 import os,urllib2,re,time,MySQLdb,sys
 reTitle          = re.compile('<font[^>]*>(.*?)<\/font><font[^>]*')
 reNeiron         = re.compile('[1-9|A-Z|a-z].*')
 retiqu          = re.compile('^(?!MARGINWIDTH|BR).*.[^>|}]$')
 rezhong          = re.compile('^[^[].*')
 shijian=1190944000
 Str1="\\n---------------- BLOG OF YAO"
 bianhao=2859
 for i in range(1,1500):
     Str2=""
     ltime=time.localtime(shijian)
     timeStr=time.strftime("%Y%m%d",ltime)
     url="http://www.jokeswarehouse.com/cgi-bin/viewjoke2.cgi?id=%s" %timeStr
     print url
     a=urllib2.urlopen(url).read()
     Title=reTitle.findall(a)
     print "=========================================================================================================="
     for titles in map(None,Title):
         titles=MySQLdb.escape_string(titles)
         print titles
     Neiron=re.findall(reNeiron,a)
     for i in map(None,Neiron):
         x=re.findall(retiqu,i)
         for str in x:
             str=MySQLdb.escape_string(str)
             Str2 += str+"\\n"
     shijian += 86400
     bianhao += 1
     try:
         conn=MySQLdb.connect("XXXX.XXXX.XXXX.XXXX","user","passwd","dbname",charset="utf8", init_command="set names utf8")
     except MySQLdb.OperationalError,message:
         print "like error"
     cursor=conn.cursor()
     sql="INSERT INTO wp_posts (post_author,post_date,post_date_gmt,post_content,post_content_filtered,post_title,post_excerpt,post_status,post_type,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_parent,menu_order,guid) VALUES (\'1\',\'2011-06-01 22:12:25\',\'2011-05-09 04:12:25\',\'\',\'\',\'Auto Draft\',\'\',\'inherit\',\'revision\',\'open\',\'open\',\'\',\'100-revision\',\'\',\'\',\'2011-06-01 22:12:25\',\'2011-05-09 04:12:25\',\'%s\',\'0\',\'\')" %bianhao
     sql2="UPDATE wp_posts SET post_author = 1, post_date = \'2011-06-01 22:12:25\', post_date_gmt = \'2011-06-01 22:12:25\', post_content =\'%s\', post_content_filtered = \'\', post_title = \'%s\', post_excerpt = \'\', post_status = \'publish\', post_type = \'post\', comment_status = \'open\', ping_status = \'open\', post_password = \'\', post_name = \'%s\', to_ping = \'\', pinged = \'\', post_modified = \'2011-06-01 22:12:25\', post_modified_gmt = \'2011-05-09 04:12:30\', post_parent = 0, menu_order = 0, guid = \'http://www.moncleronlineshops.com/?p=%s\' WHERE ID = %s" %(Str2,titles,titles,bianhao,bianhao)
     cursor.execute(sql)
     cursor.execute(sql2)
     cursor.close()
     conn.close()
     sys.exit()

下面,我们来给代码加些注释,让读者能看的更明白一些,如下:

阅读剩余部分

相关阅读 >>

wordpress中获取所使用的模板的页面id的简单方法

教你如何在wordpress发布文章时自定义文章作者名称

wordpress页面压缩 加速网站访问的方法

wordpress教程防wordpress广告的方法 评论中包含过多链接不可提交

php本地环境之phpstudy2016使用教程

wordpress简单三步实现首页文章显示缩略图(无插件)

php制作简单模版引擎

wordpress实现评论后可显示内容中附件下载地址的方法

wordpress中用于获取搜索表单的php函数使用解析

wordpress反复出现需要升级数据库的解决方法

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...