Grab data (text) from the web with Python and urllib

You know that if you want to do something, there is probably a library in Python that does it for you. For example: do you need to get a file on your computer without carrying a pen drive? You could use google drive, maybe, but what if you need the data to be used in your python code without having to enter passwords, downloading… etc? You will probably use python to do this in a fast and easy way. So if you need some data, some text, some code in a python file and you want to use it from everywhere, you should probably consider the urllib library to make it possible. So, watch the code below, it is very simple and there is a video too. I think you need only some fantasy to think to some very useful stuff you can do with this simple example, if you adapt it to your personal needs.

from urllib.request import urlopen
import os


file = "test_es1.txt"
content = urlopen("https://formazione.github.io/" + file)
text = content.read().decode("utf-8")

with open(file, "w") as newfile:
	newfile.write(text)

print("Done")
os.startfile(file)

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.