【问题标题】:Retrieve an image from a website using Ruby and Nokogiri使用 Ruby 和 Nokogiri 从网站检索图像
【发布时间】:2015-07-29 09:58:59
【问题描述】:

我正在尝试使用 Ruby 从该网站获取图像。 https://steamcommunity.com/market/listings/730/M4A1-S%20%7C%20Cyrex%20(Minimal%20Wear) 到目前为止,我已经成功获取网站上列出的项目名称的代码:

 html = Nokogiri::HTML.parse(open('https://steamcommunity.com/market/listings/730/'+url2))
 title = html.css('title').text
 titles = title.sub(/^Steam Community Market :: Listings for / , '')

结果为“M4A1-S | Cyrex(略有磨损)”

(“url2”来自我制作的html页面的输入框)

Steam 网站上的图片有一个“market_listing_largeimage”类。

有没有办法也使用 Nokogiri 来获取图像 src 以便我可以将其输入到 Html 中?

【问题讨论】:

    标签: html ruby steam marketplace


    【解决方案1】:

    图像没有该类;包裹图像的div 确实如此。也就是说,

    html.at_css('.market_listing_largeimage img')['src']
    

    【讨论】:

    • 谢谢!我试试看!
    • 如果我想将检索到的 src 放入 html,这是 html 的正确语法吗? >
    • 如果幸运的话,这将起作用,具体取决于变量的内容。 <img src="<%= bookmark.pricecheck%>"></img> 应该更健壮;但是只有在对字符串进行一些转义时,您才会完全安全。如果您使用的是 Erubis 而不是 ERb(Rails 的默认设置;如果不是,则应该使用),您可以使用 <img src="<%== bookmark.pricecheck%>"></img> 以确保完全安全(它会为您转义)。
    猜你喜欢
    • 2013-01-13
    • 2021-11-21
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2013-08-04
    • 2013-06-06
    • 2012-01-14
    相关资源
    最近更新 更多