本文整理自网络,侵删。
在学习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教程防wordpress广告的方法 评论中包含过多链接不可提交
更多相关阅读请进入《wordpress》频道 >>