在内容聚合场景中,用Ruby结合Nokogiri库模拟爬虫抓取网页内容,再导出为标准RSS种子是非常实用的需求。下面我们就一步步实现这个完整流程。

环境准备
首先确保本地已经安装了Ruby环境,然后需要安装Nokogiri和RSS相关的依赖库,执行以下命令即可完成安装:
# 安装nokogiri解析库 gem install nokogiri # 安装ruby自带的rss库,无需额外安装,标准库已包含
核心实现步骤
1. 用Nokogiri抓取目标网页内容
我们先以抓取某个博客的文章列表为例,使用Nokogiri发送HTTP请求并解析网页结构,提取需要的标题、链接、发布时间等信息:
require 'nokogiri'
require 'open-uri'
# 目标网页地址,这里用示例地址,实际使用时替换成目标站点
target_url = 'http://ipipp.com/blog/list'
begin
# 发送请求并解析网页
doc = Nokogiri::HTML(URI.open(target_url))
# 提取文章列表,根据实际网页的CSS选择器调整
articles = doc.css('.article-item')
article_list = []
articles.each do |item|
title = item.css('.article-title').text.strip
link = item.css('a').attr('href').value
# 处理相对链接,拼接成完整地址
full_link = link.start_with?('http') ? link : "http://ipipp.com#{link}"
pub_time = item.css('.pub-time').text.strip
article_list << { title: title, link: full_link, pub_time: pub_time }
end
puts "共抓取到#{article_list.size}篇文章"
rescue => e
puts "抓取失败:#{e.message}"
end2. 生成标准RSS种子
抓取到内容后,我们需要按照RSS 2.0的标准格式生成对应的种子文件,Ruby的标准库已经提供了RSS相关的生成方法:
require 'rss'
def generate_rss(article_list, channel_title, channel_link, channel_desc)
# 创建RSS 2.0版本的对象
rss = RSS::Maker.make('2.0') do |maker|
# 设置频道基础信息
maker.channel.title = channel_title
maker.channel.link = channel_link
maker.channel.description = channel_desc
maker.channel.pubDate = Time.now
# 遍历文章列表添加条目
article_list.each do |article|
maker.items.new_item do |item|
item.title = article[:title]
item.link = article[:link]
item.pubDate = Time.parse(article[:pub_time]) rescue Time.now
item.description = "来自爬虫抓取的网页内容:#{article[:title]}"
end
end
end
rss.to_s
end3. 完整流程整合
将抓取和生成的逻辑整合起来,最终输出RSS文件:
require 'nokogiri'
require 'open-uri'
require 'rss'
# 抓取网页内容的方法
def fetch_articles(target_url)
begin
doc = Nokogiri::HTML(URI.open(target_url))
articles = doc.css('.article-item')
article_list = []
articles.each do |item|
title = item.css('.article-title').text.strip
link = item.css('a').attr('href').value
full_link = link.start_with?('http') ? link : "http://ipipp.com#{link}"
pub_time = item.css('.pub-time').text.strip
article_list << { title: title, link: full_link, pub_time: pub_time }
end
article_list
rescue => e
puts "抓取失败:#{e.message}"
[]
end
end
# 生成RSS的方法
def generate_rss(article_list, channel_title, channel_link, channel_desc)
RSS::Maker.make('2.0') do |maker|
maker.channel.title = channel_title
maker.channel.link = channel_link
maker.channel.description = channel_desc
maker.channel.pubDate = Time.now
article_list.each do |article|
maker.items.new_item do |item|
item.title = article[:title]
item.link = article[:link]
item.pubDate = Time.parse(article[:pub_time]) rescue Time.now
item.description = "爬虫抓取内容:#{article[:title]}"
end
end
end.to_s
end
# 主流程执行
if __FILE__ == $0
target_url = 'http://ipipp.com/blog/list'
channel_title = '示例博客RSS订阅'
channel_link = 'http://ipipp.com/blog'
channel_desc = '通过Ruby爬虫抓取的博客最新内容订阅源'
articles = fetch_articles(target_url)
if articles.size > 0
rss_content = generate_rss(articles, channel_title, channel_link, channel_desc)
# 输出到文件
File.write('output.rss', rss_content)
puts "RSS种子已生成,保存路径:output.rss"
else
puts "未抓取到有效内容,无法生成RSS"
end
end注意事项
- 实际使用时需要根据目标网页的实际HTML结构,调整CSS选择器,避免选择器不匹配导致抓取失败
- 部分站点有反爬限制,可以适当添加请求头,或者设置请求间隔,避免被封禁IP
- 生成的RSS文件需要符合标准格式,否则订阅工具可能无法识别,可以用在线RSS验证工具检查格式合规性
- 爬虫使用需要遵守目标站点的robots协议,不要抓取禁止访问的内容,避免法律风险