Download file as string in python
Solution 1
The following example works.
from urllib.request import urlopen
url = 'http://winterolympicsmedals.com/medals.csv'
output = urlopen(url).read()
print(output.decode('utf-8'))
Alternatively, you could use requests which provides a more human readable syntax. Keep in mind that requests requires that you install additional dependencies, which may increase the complexity of deploying the application, depending on your production enviornment.
import requests
url = 'http://winterolympicsmedals.com/medals.csv'
output = requests.get(url).text
print(output)
Solution 2
In Python3.x, using package 'urllib' like this:
from urllib.request import urlopen
data = urlopen('http://www.google.com').read() #bytes
body = data.decode('utf-8')
Solution 3
Another good library for this is http://docs.python-requests.org
It's not built-in, but I've found it to be much more usable than urllib*.
Jakob Weisblat
Security Engineer @ Zoom. Puzzle Writer @ Galactic Puzzle Hunt. Theater artist, crossword constructor, vegetarian chef, enthusiast of board and role-playing games, tinkerer. pronouns: they/them
Updated on July 21, 2022Comments
-
Jakob Weisblat almost 2 years
I want to download a file to python as a string. I have tried the following, but it doesn't seem to work. What am I doing wrong, or what else might I do?
from urllib import request webFile = request.urlopen(url).read() print(webFile)
-
devoured elysium over 4 yearsThe topic was marked as 3.x. This doesn't seem to work in 3.x.
-
eandersson over 4 yearsWeird... pretty sure something changed with the question.. or maybe it worked on early versions of Python (since this is from 2013).
-
ctrueden over 3 yearsBoth example blocks work on my macOS system with Python 3.8.2.