Created
February 20, 2018 13:06
-
-
Save letroot/f525d9082e5454f6d0a0e0479e1cc3ba to your computer and use it in GitHub Desktop.
Batch download composingprogram.com pages as pdf
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
import pdfkit | |
import requests | |
from bs4 import BeautifulSoup | |
html = requests.get("http://www.composingprograms.com/") | |
soup = BeautifulSoup(html.text, 'html.parser') | |
res = [] | |
for link in soup.find_all('a'): | |
linkstr = link.get('href') | |
if './pages/' in linkstr: | |
linkstr = linkstr.replace('./pages', 'http://www.composingprograms.com/pages') | |
res.append(linkstr) | |
def urltitle(url): | |
html = requests.get(url).text | |
title = BeautifulSoup(html, 'html.parser').title.string | |
return title | |
urltitle_dict = {k: urltitle(k) for k in res} | |
pdfkit.from_url(res[0], urltitle_dict[res[0]]+'.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment