假设有一个博客名称 blog.example.com。有帖子-
<article><header><h1>Blot Title here1</h1></header></article>
<article><header><h1>Blot Title here2</h1></header></article>
<article><header><h1>Blot Title here3</h1></header></article>
使用selector Gaget,您将了解每篇文章由哪个 css 负责。要废弃文章,您可以使用 nokogiri 或 machanize gem。
假设 macanize bot 将访问 blog.example.com,它将收集所有文章并插入到您的数据库中。
require 'nokogiri'
require 'open-uri'
if 1==1
url = "http://www.eslemployment.com/country/esl-jobs-Vietnam.html"
doc = Nokogiri::HTML(open(url))
data = []
doc.css("#joblist td:nth-child(1) a").first(5).each do |titlecss|
country = "8"
jobtype = "1"
urlnext = titlecss.attr('href')
docnext = Nokogiri::HTML(open(urlnext))
docnext.css('#jobdescription div').remove
docnext.css('#detailjob , #job-summary').each do |detailscss|
docnext.css('#pagemsg h1').each do |titlenextcss|
data << JobPost.create(
:title => titlenextcss.text,
:jobslink => urlnext,
:description => detailscss.inner_html,
:country_id => country,
:job_type_id => jobtype
)
end
end
end
end
这是一个 nokogiri gem 的例子。它从 www.eslemployment.com 收集工作。现在您的问题是如何检测新文章已添加。
此代码收集页面中的所有作业并将其添加到数据库中。我在这里使用“distint”代码到模型中,因此只有新作业才会添加到数据库中。不会将复制作业添加到数据库中。添加新作业时,您可以通知添加了哪个作业。
这不是有效的方法。但它会起作用。否则,您可以使用该博客的 RSS 提要。这是检测新帖子的正确方法。