代码之家  ›  专栏  ›  技术社区  ›  corybantic

使用Ruby和Nokogiri从网站检索图像

  •  0
  • corybantic  · 技术社区  · 9 年前

    我正在尝试使用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中?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Amadan    9 年前

    图像没有该类;这个 div 图像被包裹在其中。也就是说,

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