SoFunction
Updated on 2024-11-21

python crawl latest blog content and generate Rss

oc rss is not full-text output, not happy, so there is a python capture oc latest blog to generate Rss

# -*- coding: utf-8 -*-


from bs4 import BeautifulSoup
import urllib2

import datetime
import time
import PyRSS2Gen
from  import formatdate
import re
import sys
import os
reload(sys)
('utf-8')

class RssSpider():
 def __init__(self):
  = PyRSS2Gen.RSS2(title='OSChina',
link='',
description=str(()),
pubDate=(),
 lastBuildDate = (),
items=[]
)
=r'/var/www/myrss/'

="/blog"
 #if ():
#()
 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": '/'}
 req = (url, headers=i_headers)
 html = (req).read()
 return html
 def enterpage(self,url):
 pattern = (r'd{4}Sd{2}Sd{2}sd{2}Sd{2}')
rsp=(url)
soup=BeautifulSoup(rsp)
timespan=('div',{'class':'BlogStat'})
timespan=str(timespan).strip().replace('n','').decode('utf-8')
match=(r'd{4}Sd{2}Sd{2}sd{2}Sd{2}',timespan)
timestr=str(())
 if match:
timestr=()
 #print timestr
ititle=
div=('div',{'class':'BlogContent'})
rss=(
title=ititle,
link=url,
 description = str(div),
 pubDate = timestr
)

 return rss
 def getcontent(self):
rsp=()
soup=BeautifulSoup(rsp)
ul=('div',{'id':'RecentBlogs'})
 for li in ('li'):
div=('div')
 if div is not None:
alink=('a')
 if alink is not None:
link=('href')
 print link
html=(link)
(html)
 def SaveRssFile(self,filename):
finallxml=.to_xml(encoding='utf-8')
file=open(,'w')
(finallxml)
()



if __name__=='__main__':
rssSpider=RssSpider()
()
('')

The above mentioned is the whole content of this article, I hope you will enjoy it.