【发布时间】:2014-11-07 22:02:35
【问题描述】:
我正在开发一个 Rails 应用程序,在该应用程序中我从 BBC 获取新闻报道的 rss 提要。然后,我存储来自 rss 提要的新闻报道的标题、摘要和 url。起初我的数据库中有一个空白列的图像,因为我访问了故事的 url,然后使用 nokogiri 通过 rake 任务刮取了主图像的 url。获得图像的 url 后,我尝试将具有新更新属性的项目保存到我的数据库中。但是,每当我运行我的 rake 任务时,它都不起作用并且出现错误。代码如下:
task :getimg => :environment do
stories = FeedEntry.all
stories.each do |story|
url = story.url
doc = Nokogiri::HTML(open(url))
if doc.at_css(".full-width img")
img = doc.at_css(".full-width img")[:src]
story.image = img #my attempt to update the story's attribute
story.save! #does this save work?
elsif doc.at_css(".body-width img")
img = doc.at_css(".body-width img")[:src]
story.image = img # my attempt to update the story's attribute
story.save! #does this save work?
end
end
end
FeedEntry 是我的新闻报道模型,这是控制台中的输出:
rake getimg --trace
** Invoke getimg (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute getimg
rake aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - http://www.bbc.co.uk/news/world-africa-29184590#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa
/Users/abhasarya/rails_projects/news_reader/lib/tasks/image_scraper.rake:9:in `initialize'
我知道我做错了什么,如果有人能指出我的解决方案,我将不胜感激。谢谢!
【问题讨论】:
标签: ruby-on-rails ruby rss nokogiri