python抓取最新博客内容并生成Rss
2015-05-17 00:00
791 查看
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')
以上所述就是本文的全部内容了,希望大家能够喜欢。
相关文章推荐
- python抓取最新博客内容并生成Rss
- 抓取糗百内容小爬虫(python2.7) 2015最新版
- Python 抓取「知识星球」内容生成电子书
- python抓取javascript动态生成HTML内容的实践
- Python抓取知乎答案内容
- 萌新的Python学习日记 - 爬虫无影 - 添加headers抓取动态网页内容:TripAdvisor(上)
- 利用python3使用urllib通过指定的URL抓取网页内容
- 抓取网页内容生成kindle电子书
- Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 【python】爬虫3——抓取亦舒博客所有文章
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容(转)
- 如何用python抓取js生成的数据 - SegmentFault
- [python]抓取网页的内容
- python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容
- python 抓取网页内容教程
- python rss解析与生成
- C#抓取网面上的html内容(JS动态生成的无法抓取)
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取动态网页内容:Knewone