Search code examples
htmlrubysteammarketplace

Retrieve an image from a website using Ruby and Nokogiri


I am trying to get an image from this website using Ruby. https://steamcommunity.com/market/listings/730/M4A1-S%20%7C%20Cyrex%20(Minimal%20Wear) So far, I have successful code to get the name of the item listed on the website:

 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 / , '')

Which results in "M4A1-S | Cyrex (Minimal Wear)"

(The "url2" comes from an input box on the html page that I made)

The image on the Steam Website has a class of "market_listing_largeimage".

Is there a way to also use Nokogiri to get the image src so that I can then input it into Html?


Solution

  • The image does not have that class; the div that the image is wrapped in does. That said,

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