Skip to content

Instantly share code, notes, and snippets.

@maxcohn
Created May 16, 2019 14:44
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 maxcohn/bf226e870c92cdb1f362168d000a6e76 to your computer and use it in GitHub Desktop.
Save maxcohn/bf226e870c92cdb1f362168d000a6e76 to your computer and use it in GitHub Desktop.
# Downloads all Project Euler problems and stores them in a file with their number
#
# These files can be changed to any language's file extension, and comment the text out
# to have a nicely formatted file
#
# Requirements: BeautifulSoup4 and requests
from bs4 import BeautifulSoup
import re
import requests
# timeplate = https://projecteuler.net/problem=1
# change this as new problems are added
last_prob = 669
# main loop
for i in range(1, last_prob + 1):
# interpolate new url
url = f'https://projecteuler.net/problem={i}'
# get the page and get a BS object
page = requests.get(url).content
soup = BeautifulSoup(page, 'html.parser')
# get the problem name, number, and text from the page
title = soup.select_one('#content h2').text
prob_no = re.search(r'\d+', soup.select_one('#content #problem_info h3').text).group(0)
prob = soup.select_one('.problem_content').text
# write everything to a file
with open(f'pe-{prob_no}.txt', 'w+', encoding="utf-8") as f:
f.write(title + '\n\n')
f.write(f'Problem #{prob_no}\n\n')
f.write(prob)
print(f'Finished #{prob_no}')
print('Finished!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment