Skip to content

Instantly share code, notes, and snippets.

@subintp
Created November 13, 2023 16:17
Show Gist options
  • Save subintp/c8b743bef37c96f5d7cf92cff941a773 to your computer and use it in GitHub Desktop.
Save subintp/c8b743bef37c96f5d7cf92cff941a773 to your computer and use it in GitHub Desktop.
g2_review_scrapper.rb
def scrape_reviews(review_url, pages)
for i in 1..14 do
results = scraper(review_url+"&page="+i.to_s)
append_to_file(results)
end
end
def scraper(url)
results = []
browser = Watir::Browser.new :chrome
browser.goto(url)
sleep(25)
browser.elements(css: '.nested-ajax-loading > div.paper').each do |element|
reviewer_name = element.element(css: "[itemprop=author]").text
profile_title = element.elements(css: '.mt-4th').map(&:text).first
stars = element.element(css: "[itemprop='ratingValue']").attribute('content')
review_date = element.element(css: '.x-current-review-date').text
review_link = element.element(css: '.pjax').attribute('href')
results << [reviewer_name, profile_title, review_link, stars, review_date ]
end
browser.close
results
end
def append_to_file(data)
File.open('reviews.csv', 'a') do |file|
data.each do |d|
file.puts d.join(",")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment