Created
May 13, 2023 22:18
-
-
Save IanVaughan/4a26b2ea919ae748595b0d89cb5e9977 to your computer and use it in GitHub Desktop.
Scrape discogs.com price data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'pry' | |
urls = File.read('urls.txt').split("\n") | |
CSV.open('scraped_data.csv', 'wb') do |csv| | |
urls.each do |url| | |
html = URI.open(url).read | |
doc = Nokogiri::HTML(html) | |
puts(url) | |
median = doc.xpath("//*[contains(text(), 'Median')]").first.parent.children.last.children.last.to_s | |
for_sale = doc.xpath("//*[contains(text(), 'For Sale')]")[1].parent.children.last.children.last.to_s | |
csv << [url, median, for_sale] | |
rescue StandardError | |
csv << [url] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment