In one line, after you imported urllib, you can download the whole Shakespeare masterpiece “Romeo and Juliet” for free! This is great. Of course you can download everything is possible to download with the same code, changing the url of the file you want to download. Just add the address and the name with which you want to save locally the file. Just use urlretrieve and you’re done. That is why we love Python: semplicity and intuitive methods names. Let’s see the code then.
from urllib import request
request.urlretrieve("https://www.gutenberg.org/files/1112/1112.txt","romeo_n_juliet.txt")
It was easy, right? Have fun with the reading of this classic of classic.
Download all the Shakespeare works
In this script, you can download 2 works from shakespeare and if you want you can add as many as you want.
Just remember:
- add an empty line among books
- write the name of the txt file in the first line
- write the address where the txt file is in the second line
- add another empty line and go on like this for other books
from urllib import request import os def download_shakespeare(books): "Download all the files in the books multiline string" books = books[1:].split("\n\n") books = [x.split("\n") for x in books] print(books) title = [data[0] for data in books] address = [data[1] for data in books] print(title) for n, x in enumerate(books): if title[n] not in os.listdir(): request.urlretrieve(address[n], title[n]) os.startfile(title[n]) books = """ Hamlet.txt https://gist.githubusercontent.com/provpup/2fc41686eab7400b796b/raw/b575bd01a58494dfddc1d6429ef0167e709abf9b/hamlet.txt Romeo_and_Juliet.txt https://www.gutenberg.org/files/1112/1112.txt Othello.txt https://formazione.github.io/books/Othello.txt """""" download_shakespeare(books)
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame