Last active
May 30, 2018 02:10
-
-
Save cosmoscalibur/5ebedb0eb245e207f89ae27cc46df5ba to your computer and use it in GitHub Desktop.
Automatic download of Spanish books from Guttenberg project.
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
""" | |
This script help you to download the large collection of Spanish books included | |
in `Guttenberg project <https://www.gutenberg.org/browse/languages/es>`_. | |
You need to install `requests-html <http://html.python-requests.org/>`_. | |
:Authors: | |
Edward Villegas-Pulgarin <cosmoscalibur at gmail dot com>. | |
""" | |
from requests_html import HTMLSession | |
session = HTMLSession() | |
guttenberg_site = session.get('https://www.gutenberg.org/browse/languages/es') | |
text_items = guttenberg_site.html.find('.pgdbetext') | |
total = len(text_items) | |
book = 1 | |
failed = 0 | |
for text in text_items: | |
link = text.absolute_links.pop() | |
download_link = link + '.txt.utf-8' | |
file_name = link.split('/')[-1] + '.txt' | |
tries = 1 | |
succes = False | |
print("Downloading {} ({} of {})".format(file_name, book, total)) | |
while (tries <= 5) and (succes == False): | |
try: | |
text_stream = session.get(download_link) | |
succes = True | |
except: | |
tries = tries + 1 | |
print("Try {}".format(tries)) | |
if succes: | |
downloaded_file = open(file_name, "wb") | |
for chunk in text_stream.iter_content(chunk_size=512): | |
if chunk: | |
downloaded_file.write(chunk) | |
downloaded_file.close() | |
else: | |
print("Failed download.") | |
failed = failed + 1 | |
book = book + 1 | |
print("{} books not downloaded".format(failed)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment