用python寫的一個(gè)wordpress的采集程序
在學(xué)習(xí)python的過程中,經(jīng)過不斷的嘗試及努力,終于完成了第一個(gè)像樣的python程序,雖然還有很多需要優(yōu)化的地方,但是目前基本上實(shí)現(xiàn)了我所要求的功能,先貼一下程序代碼:
具體代碼如下:
#! /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()
下面,我們來給代碼加些注釋,讓讀者能看的更明白一些,如下:
具體代碼如下
#! /usr/bin/python import os,urllib2,re,time,MySQLdb,sys #加載本程序需要調(diào)用的相模塊 reTitle = re.compile('<font[^>]*>(.*?)<\/font> <font[^>]*') # 定義一下取文章標(biāo)題的正則 reNeiron = re.compile('[1-9|A-Z|a-z].*') #定義一個(gè)取提取文章內(nèi)容的正則(注:這里提取出來的不是很精細(xì),需要在下面的正則里,再進(jìn)行提取,這里只是取一個(gè)大概) retiqu = re.compile('^(?!MARGINWIDTH|BR).*.[^>|}]$') #這里定義一個(gè)正則,將上面reNeiron提取出來的字符,再進(jìn)行細(xì)化。 shijian=1190944000 #這里字義了一個(gè)時(shí)間戳, Str1="\\n---------------- BLOG OF YAO" #這個(gè)沒用,開始是準(zhǔn)備加到文章里的,后來沒加進(jìn)去。 bianhao=2859 #這里是wordpress 的文章編號(hào),直接查看wp-posts表的id 字段的最后一個(gè)數(shù)字。 for i in range(1,1500): #循環(huán)1500遍,也就是采集1500篇文章。 Str2="" #先賦值給Str2 空值 ltime=time.localtime(shijian) timeStr=time.strftime("%Y%m%d",ltime) #這兩句將上面的時(shí)間戳改為時(shí)間,樣式為19700101這樣的格式 url="http://www.jokeswarehouse.com/cgi-bin/viewjoke2.cgi?id=%s" %timeStr #定義要采集的網(wǎng)站,將轉(zhuǎn)化后的時(shí)間放在這個(gè)url的最后。 a=urllib2.urlopen(url).read() #將這個(gè)網(wǎng)頁的源代碼讀出來,賦值給a; Title=reTitle.findall(a) #使用 reTitle這個(gè)正則提取出標(biāo)題 print "==========================================================================================================" for titles in map(None,Title): #上面提取出來的標(biāo)題前后都有一個(gè) [] 所以我們要寫個(gè)for循環(huán)把前后的[]去掉,并轉(zhuǎn)義成能直接插入mysql庫的格式。 titles=MySQLdb.escape_string(titles) Neiron=re.findall(reNeiron,a) #先用reNeiron,取個(gè)大概的內(nèi)容模型出來。這些都是以逗號(hào)分隔的數(shù)組。 for i in map(None,Neiron): # 我們來循環(huán)讀出Neiron這個(gè)數(shù)組里的每個(gè)值。 x=re.findall(retiqu,i)#并用 retiqu這個(gè)正則提出精細(xì)出的內(nèi)容。 for str in x: str=MySQLdb.escape_string(str) Str2 += str+"\\n" #利用這個(gè)循環(huán),我們把內(nèi)容加到一起,并賦值給Str2這個(gè)變量,這個(gè) Str2這個(gè)變量就是所有的文章內(nèi)容。 shijian += 86400 #每循環(huán)一次,就把shijian這個(gè)變量加上一天。 bianhao += 1 #每循環(huán)一次,就把bianhao這個(gè)變量加上一 try: #下面是用mysqldb連接數(shù)據(jù)庫,并嘗試連接是否成功。 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() #下面是插入wordpress數(shù)據(jù)庫的兩條語句,我是從mysqlbinlog里面導(dǎo)出來的,測試是可以插入數(shù)據(jù)庫,并能正常把內(nèi)容顯示在網(wǎng)頁的。變量都寫在這兩條語句里。 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) #連接數(shù)據(jù)庫并執(zhí)行這兩條語句。 cursor.close() conn.close() #關(guān)閉數(shù)據(jù)庫。 sys.exit()
上面是程序的代碼,采集的是:www.jokeswarehouse.com 的一個(gè)笑話網(wǎng)站。通過 python 的 re 模塊,也就是正則匹配模塊,運(yùn)行相應(yīng)的正則表達(dá)式,進(jìn)行過濾出我們所需要的標(biāo)題和文章內(nèi)容,再運(yùn)用 python 的mysqldb 模塊,進(jìn)行連接數(shù)據(jù)庫,利用相應(yīng)的插入語句,進(jìn)行插入數(shù)據(jù)庫。
相關(guān)文章
解決Python保存文件名太長OSError: [Errno 36] File
這篇文章主要介紹了解決Python保存文件名太長OSError: [Errno 36] File name too lon問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Python中實(shí)現(xiàn)遠(yuǎn)程調(diào)用(RPC、RMI)簡單例子
說白了,遠(yuǎn)程調(diào)用就是將對(duì)象名、函數(shù)名、參數(shù)等傳遞給遠(yuǎn)程服務(wù)器,服務(wù)器將處理結(jié)果返回給客戶端2014-04-04Python生成指定數(shù)量的優(yōu)惠碼實(shí)操內(nèi)容
在本篇文章里小編給大家整理了關(guān)于Python生成指定數(shù)量的優(yōu)惠碼的實(shí)例內(nèi)容以及相關(guān)代碼,有需要的朋友們學(xué)習(xí)下。2019-06-06通過 Django Pagination 實(shí)現(xiàn)簡單分頁功能
這篇文章主要介紹了通過 Django Pagination 實(shí)現(xiàn)簡單分頁功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Python字符串字母大小寫轉(zhuǎn)換的各種情況詳析
在使用python語言開發(fā)中經(jīng)常會(huì)碰到,需要大寫轉(zhuǎn)小寫,小寫轉(zhuǎn)換大寫,甚至字符串中的單詞首字母大寫,以及字符串手字字母大寫的問題,下面這篇文章主要給大家介紹了關(guān)于Python字符串字母大小寫轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2022-05-05利用python實(shí)現(xiàn)xml與數(shù)據(jù)庫讀取轉(zhuǎn)換的方法
這篇文章主要給大家介紹了關(guān)于利用python實(shí)現(xiàn)xml與數(shù)據(jù)庫讀取轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06Python實(shí)戰(zhàn)使用Selenium爬取網(wǎng)頁數(shù)據(jù)
這篇文章主要為大家介紹了Python實(shí)戰(zhàn)使用Selenium爬取網(wǎng)頁數(shù)據(jù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2023-05-05