Rails:如何从 URL 中读取图像列表

Rails: How to read a list of images from a URL

我有一个 URL 如下所示->

images = open("example.com").read

哪个returns

<center>
<font size=-1>
<img src=example.com/show?1><br>1 image<p>
<img src=example.com/show?2><br>2 image<p>
<img src=example.com/show?3><br>3 image<p>
</font>

我想在后端捕获这些中的每一个并将它们发送到前端。 到目前为止,我将生成的 html 直接发送到显示它的前端。但现在我想在后端捕获它,然后将每一个发送到 UI。我该怎么做?

为此,我会推荐 Nokogiri。然后你可以做一些像

html_string = open("example.com").read
nokogiri_html_string = Nokogiri::HTML( html_string )
image_tags = nokogiri_html_string.css('img')
image_sources = nokogiri_html_string.css('img').map{ |i| i['src'] }

希望这会有所帮助。