Skip to content

Instantly share code, notes, and snippets.

@geoffnin
Created December 14, 2013 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffnin/7955987 to your computer and use it in GitHub Desktop.
Save geoffnin/7955987 to your computer and use it in GitHub Desktop.
Simple python script to download Project Euler problems into a new directory. Requires BeautifulSoup4, which can be installed using pip install beautifulsoup4
import sys, os, re
import urllib2
from bs4 import BeautifulSoup
euler_url = 'http://projecteuler.net/problem='
def main():
for problem_number in sys.argv[1:]:
page = urllib2.urlopen(euler_url + problem_number)
html = page.read()
soup = BeautifulSoup(html)
# scrape the problem title
title = soup.find(id='content').h2.get_text()
# scrape problem statement
content = soup.find(id='content').find(attrs={'class': re.compile(r".*\bproblem_content\b.*")})
# directory structure is:
# [problem_number]_[title]
# solution.py
# problem.html
dir_name = problem_number + "_" + title.strip().lower().replace(' ', '_')
# make problem directory if not exists
if not os.path.exists(dir_name):
os.makedirs(dir_name)
# create the problem statement file
with open(os.path.join(dir_name, 'problem.html')) as problem_file:
problem_file.write(content)
# create the solution script with scaffolding
with open(os.path.join(dir_name, 'solution.py')) as solution_file:
# write the python script scaffolding here
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment