Python crawls the latest blog content and generates Rss; python crawls blog rss

Source: Internet
Author: User

Python crawls the latest blog content and generates Rss; python crawls blog rss

The rss of osc is not output in full text, so it is not happy that python captures the latest osc blog to generate 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/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 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')

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.