How can I extract image from a website into my rails project?

时间:2021-07-07 00:34:39

I am very new to Ruby on rails. I am making a web application in which I have to fetch RSS feeds from news websites, which I have done using gem feedzirra. I was thinking of extracting images from every feed url and posting it on my application. But I don't know which gem will be suitable for just extracting images from different websites. I thought of using nokogiri or opengraph but I don't know how to go about it. Can someone please guide me. Thank you

我对Ruby on rails非常新。我正在制作一个Web应用程序,我必须从新闻网站获取RSS源,我使用gem feedzirra完成了这些。我正在考虑从每个Feed网址中提取图片并将其发布到我的应用程序中。但我不知道哪种宝石适合从不同的网站中提取图像。我想过使用nokogiri或opengraph,但我不知道如何去做。有人可以指导我。谢谢

I want to do something like this :

我想做这样的事情:

 <% @feed = Feedzirra::Feed.fetch_and_parse("http://feeds.bbci.co.uk/news/rss.xml") %>
 <% @feed.inspect %> 
 <% @entries = @feed.entries %>
 <% for i in 0..9 %> 
 <br>
 <b><u><%= @entries[i].title %></u></b>
 </br>
 <br>
 **<%= get image from @entries[i].url %> \\ how can i do this ?**
 </br>
 <% end %>

1 个解决方案

#1


1  

It can be achieved using ActionView image_tag helper, which would create a html img tag to an external url in this case:

可以使用ActionView image_tag helper来实现,在这种情况下,它会为外部URL创建一个html img标记:

...
<% @entries.each do |entry| %>
  <%= entry.title %>
  <%= image_tag(entry.url) %>
<% end %>

Also, please take into consideration more Rubyish way to iterate through Array elements, which I presented above.

另外,请考虑更多Rubyish方式迭代Array元素,我在上面提到过。

If you would like to iterate through first 10 elements, instead of the whole array (like in your example) you could do it in the following way:

如果您想迭代前10个元素,而不是整个数组(如您的示例中所示),您可以通过以下方式执行此操作:

<% @entries[0..9].each do |entry| %>
  <%= entry.title %>
  <%= image_tag(entry.url) %>
<% end %> 

Edit:

It seems like you can get img tag from the entry summary using Nokogiri, haven't tested it myself but you should check this blog post: http://verboselogging.com/2009/05/24/rss-fun-with-feedzirra

看起来你可以使用Nokogiri从入门摘要中获取img标签,没有自己测试过但你应该查看这篇博文:http://verboselogging.com/2009/05/24/rss-fun-with-feedzirra

#1


1  

It can be achieved using ActionView image_tag helper, which would create a html img tag to an external url in this case:

可以使用ActionView image_tag helper来实现,在这种情况下,它会为外部URL创建一个html img标记:

...
<% @entries.each do |entry| %>
  <%= entry.title %>
  <%= image_tag(entry.url) %>
<% end %>

Also, please take into consideration more Rubyish way to iterate through Array elements, which I presented above.

另外,请考虑更多Rubyish方式迭代Array元素,我在上面提到过。

If you would like to iterate through first 10 elements, instead of the whole array (like in your example) you could do it in the following way:

如果您想迭代前10个元素,而不是整个数组(如您的示例中所示),您可以通过以下方式执行此操作:

<% @entries[0..9].each do |entry| %>
  <%= entry.title %>
  <%= image_tag(entry.url) %>
<% end %> 

Edit:

It seems like you can get img tag from the entry summary using Nokogiri, haven't tested it myself but you should check this blog post: http://verboselogging.com/2009/05/24/rss-fun-with-feedzirra

看起来你可以使用Nokogiri从入门摘要中获取img标签,没有自己测试过但你应该查看这篇博文:http://verboselogging.com/2009/05/24/rss-fun-with-feedzirra