This article to share is the use of Python to crawl the latest blog content and generate RSS code, the main use of the Pyrss2gen method, very simple and practical, the need for small partners can refer to.
OSC RSS is not full-text output, not happy, so there is the Python crawl OSC latest blog to generate RSS
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80-81 |
#-*-Coding:utf-8-*- from BS4 import beautifulsoup import urllib2 Import datetime import Time Imp Ort 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.comp Ile (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=s Oup.find (' div ', {' id ': ' recentblogs '}) for Li in Ul.findall (' Li '): Div=li.find (' div ') if Div isn't 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 Sav Erssfile (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 ') |
The above is the entire contents of this article, I hope you can enjoy.