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

作者:hebedich 时间:2022-06-18 08:38:40 

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
0
投稿

猜你喜欢

  • 使用豆瓣源来安装python中的第三方库方法

    2023-10-09 08:53:42
  • Ubuntu16安装Python3.9的实现步骤

    2021-01-23 14:04:53
  • python 远程统计文件代码分享

    2023-04-13 17:02:08
  • python中文分词,使用结巴分词对python进行分词(实例讲解)

    2023-03-15 13:37:30
  • python 文件操作api(文件操作函数)

    2023-11-19 08:58:38
  • python中文件操作与异常的处理图文详解

    2021-09-04 16:04:33
  • Python数据分析基础之文件的读取

    2022-10-16 21:25:21
  • Python 实现将某一列设置为str类型

    2022-07-27 03:20:12
  • Python函数基础

    2022-09-11 11:36:43
  • 原创一个js对联广告类(兼容FireFox)

    2008-08-01 18:08:00
  • Django rest framework分页接口实现原理解析

    2023-08-22 21:26:36
  • 十分钟搞定pandas(入门教程)

    2023-08-09 01:00:15
  • pytest-fixture简介及其用法讲解

    2023-02-13 19:27:25
  • python+selenium实现12306模拟登录的步骤

    2021-06-18 15:32:56
  • Python如何使用cv2.canny进行图像边缘检测

    2021-03-11 20:38:12
  • 实例讲解MySQL数据库中文问题的解决方法

    2008-12-31 15:15:00
  • 逐步讲解向Access数据库上传且显示图片

    2008-11-28 16:51:00
  • 用Pycharm实现鼠标滚轮控制字体大小的方法

    2023-02-08 15:34:32
  • 基于Python3中运算符 **和*的区别说明

    2023-12-17 02:22:06
  • PHP+JS实现文件分块上传的示例代码

    2023-06-12 00:04:07
  • asp之家 网络编程 m.aspxhome.com