python抓取最新博客内容并生成Rss

时间:2021-05-22

osc的rss不是全文输出的,不开心,所以就有了python抓取osc最新博客生成Rss

# -*- coding: utf-8 -*-from bs4 import BeautifulSoupimport urllib2import datetimeimport timeimport PyRSS2Genfrom email.Utils import formatdateimport reimport sysimport osreload(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/pile(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 timestrititle=soup.title.stringdiv=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 linkhtml=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')

以上所述就是本文的全部内容了,希望大家能够喜欢。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章