Techioz Blog

Ruby の Web スクレイピングでは 2 つの数値が得られますが、必要なのは 1 つだけです

概要

<span style=\"color: #888888;\"><strike>$44.99</strike></span><br>$15.29

5.29のみを経由してスクレイピングしようとしています

@parse_page.css('.search_results').css('.responsive_search_name_combined').css('.col.search_price_discount_combined.responsive_secondrow').css('.col.search_price.discounted.responsive_secondrow').text

しかし、代わりに「4.995.29」が表示されます。 5.29のみを入手する方法を誰か教えてください。 参考リンク: https://store.steampowered.com/search/?filter=weeklongdeals

これは新しいコードですが、まだ機能しません。

解決策

価格 5.29 のみを収集しようとしている場合は、より正確な CSS セレクターまたは XPath 式を使用して、必要な価格のみをターゲットにすることができます。 XPath を使用してこれを行う方法は次のとおりです。

require 'nokogiri'

require 'open-uri'

#Fetch the page content

page_content = open('https://store.steampowered.com/search/?filter=weeklongdeals').read

#Parse the HTML

parse_page = Nokogiri::HTML(page_content)

#Extract the price using the provided XPath


price = parse_page.at('//div[contains(@class,""discount_original_price"")]/text()').text.strip

puts price

今なら5.29インチが手に入る