python抓取最新博客內(nèi)容并生成Rss
更新時間:2015年05月17日 12:02:44 投稿:hebedich
本文給大家分享的是使用python抓取最新博客內(nèi)容并生成Rss的代碼,主要用到了PyRSS2Gen方法,非常的簡單實用,有需要的小伙伴可以參考下。
osc的rss不是全文輸出的,不開心,所以就有了python抓取osc最新博客生成Rss
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup import urllib2 import datetime import time import PyRSS2Gen from email.Utils import formatdate import re import sys import os reload(sys) sys.setdefaultencoding('utf-8') class RssSpider(): def __init__(self): self.myrss = PyRSS2Gen.RSS2(title='OSChina', link='http://my.oschina.net', description=str(datetime.date.today()), pubDate=datetime.datetime.now(), lastBuildDate = datetime.datetime.now(), items=[] ) self.xmlpath=r'/var/www/myrss/oschina.xml' self.baseurl="http://www.oschina.net/blog" #if os.path.isfile(self.xmlpath): #os.remove(self.xmlpath) def useragent(self,url): i_headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36", "Referer": 'http://baidu.com/'} req = urllib2.Request(url, headers=i_headers) html = urllib2.urlopen(req).read() return html def enterpage(self,url): pattern = re.compile(r'd{4}Sd{2}Sd{2}sd{2}Sd{2}') rsp=self.useragent(url) soup=BeautifulSoup(rsp) timespan=soup.find('div',{'class':'BlogStat'}) timespan=str(timespan).strip().replace('n','').decode('utf-8') match=re.search(r'd{4}Sd{2}Sd{2}sd{2}Sd{2}',timespan) timestr=str(datetime.date.today()) if match: timestr=match.group() #print timestr ititle=soup.title.string div=soup.find('div',{'class':'BlogContent'}) rss=PyRSS2Gen.RSSItem( title=ititle, link=url, description = str(div), pubDate = timestr ) return rss def getcontent(self): rsp=self.useragent(self.baseurl) soup=BeautifulSoup(rsp) ul=soup.find('div',{'id':'RecentBlogs'}) for li in ul.findAll('li'): div=li.find('div') if div is not None: alink=div.find('a') if alink is not None: link=alink.get('href') print link html=self.enterpage(link) self.myrss.items.append(html) def SaveRssFile(self,filename): finallxml=self.myrss.to_xml(encoding='utf-8') file=open(self.xmlpath,'w') file.writelines(finallxml) file.close() if __name__=='__main__': rssSpider=RssSpider() rssSpider.getcontent() rssSpider.SaveRssFile('oschina.xml')
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
將python字符串轉(zhuǎn)化成長表達式的函數(shù)eval實例
這篇文章主要介紹了將python字符串轉(zhuǎn)化成長表達式的函數(shù)eval實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python中判斷input()輸入的數(shù)據(jù)的類型
在pyhton中,經(jīng)常會用到input()語句,但是input()語句輸入的內(nèi)容只能是字符串類型,而我們經(jīng)常要輸入int類型的數(shù)據(jù)等,這個時候就需要用到int()方法給輸入的內(nèi)容強制轉(zhuǎn)換,今天小編給大家介紹下Python中判斷input()輸入的數(shù)據(jù)的類型,感興趣的朋友跟隨小編一起看看吧2022-11-11Numpy 數(shù)組操作之元素添加、刪除和修改的實現(xiàn)
本文主要介紹了Numpy 數(shù)組操作之元素添加、刪除和修改的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03一文帶你掌握Python內(nèi)置reversed函數(shù)的使用
Python作為一門強大的編程語言,提供了許多內(nèi)置函數(shù)來處理各種數(shù)據(jù)結(jié)構(gòu)和對象,本文將詳細探討reversed函數(shù)的用法、示例代碼以及實際應用場景,需要的可以參考下2024-01-01Python接口自動化淺析logging日志原理及模塊操作流程
這篇文章主要為大家介紹了Python接口自動化系列文章淺析logging日志原理及模塊操作流程,文中詳細說明了為什么需要日志?日志是什么?以及日志用途等基本的原理2021-08-08