Last active
December 19, 2015 20:48
-
-
Save Ank13/6015527 to your computer and use it in GitHub Desktop.
This is a scraper to find the links on each challenge page in Socrates and export key details to a CSV file. To make this work, you need log in to Socrates on your browser, copy the appropriate Cookie from the headers (open the console and inspect headers to find your cookie), and paste into line 11 below.
This file contains 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 'nokogiri' | |
require 'open-uri' | |
require 'csv' | |
challenges = [] | |
start_challenge_id = 1 | |
end_challenge_id = 412 | |
# Searches the given page for links and returns hash of result | |
def challenge_record_from_page challenge_id | |
cookie = '_dbc_socrates_session=PASTE_YOUR_COOKIE_HERE' | |
page_url = "http://socrates.devbootcamp.com/challenges/#{challenge_id}" | |
begin | |
target = open(page_url, "Cookie" => cookie) | |
page = Nokogiri::HTML(target) | |
title = page.search('h1').text.strip.sub("\n", " ") | |
rescue | |
title = nil | |
end | |
if title.nil? || title.match('Activity Feed') | |
print "...no access to #{challenge_id}..." | |
nil | |
else | |
links = page.search('#objectives a') | |
print "...saving #{challenge_id}..." | |
{title: title, links: links, challenge_id: challenge_id} | |
end | |
end | |
# Defines range of pages to look at and iterates through | |
start_challenge_id.upto(end_challenge_id) do |i| | |
challenge_record = challenge_record_from_page i | |
challenges << challenge_record unless challenge_record.nil? | |
end | |
# Export records to allow importing into Google Drive | |
CSV.open('links2.csv', 'wb') do |csv| | |
puts "Saving to CSV now" | |
challenges.each do |challenge| | |
unless challenge.nil? | |
challenge[:links].each do |link| | |
csv << [challenge[:challenge_id], challenge[:title], link.children.text, link['href']] | |
end | |
end | |
end | |
end | |
puts 'done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment