• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python抓取最新博客内容并生成Rss

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

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python rss,rss抓取,rss抓取是什么意思,rss新闻抓取,博客rss等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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')
</div>

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

</div>

您可能想查找下面的文章:

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

相关文章

  • 在Python中处理日期和时间的基本知识点整理汇总
  • 举例讲解Python设计模式编程中的访问者与观察者模式
  • Python使用PDFMiner解析PDF代码实例
  • Python 学习笔记
  • python基于隐马尔可夫模型实现中文拼音输入
  • Python使用multiprocessing实现一个最简单的分布式作业调度系统
  • tensorflow estimator 使用hook实现finetune方式
  • 用Python写的图片蜘蛛人代码
  • python持久性管理pickle模块详细介绍
  • Python每天必学之bytes字节

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python脚本实时处理log文件的方法
    • python 回调函数和回调方法的实现分析
    • 浅析python中的分片与截断序列
    • Python XML RPC服务器端和客户端实例
    • pyenv命令管理多个Python版本
    • Python的SQLalchemy模块连接与操作MySQL的基础示例
    • Python六大开源框架对比
    • python之yield表达式学习
    • python和pyqt实现360的CLable控件
    • 十个Python程序员易犯的错误

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有