How to get file from url in python?

Question:

I want to download text files using python, how can I do so?

I used requests module’s urlopen(url).read() but it gives me the bytes representation of file.

Asked By: Yaver Javid

||

Answers:

When downloading text files with python I like to use the wget module

import wget

remote_url = 'https://www.google.com/test.txt'

local_file = 'local_copy.txt'

wget.download(remote_url, local_file)

If that doesn’t work try using urllib

from urllib import request

remote_url = 'https://www.google.com/test.txt'

file = 'copy.txt'

request.urlretrieve(remote_url, file)

When you are using the request module you are reading the file directly from the internet and it is causing you to see the text in byte format. Try to write the text to a file then view it manually by opening it on your desktop

import requests

remote_url = 'test.com/test.txt'

local_file = 'local_file.txt'

data = requests.get(remote_url)

with open(local_file, 'wb')as file:
file.write(data.content)

Answered By: Aiden Hanney

For me, I had to do the following (Python 3):

from urllib.request import urlopen

data = urlopen("[your url goes here]").read().decode('utf-8')

# Do what you need to do with the data.
Answered By: Rizwan Saleem

You can use multiple options:

  1. For the simpler solution you can use this

     file_url = 'https://someurl.com/text_file.txt'
     for line in urllib.request.urlopen(file_url):
         print(line.decode('utf-8')) 
    
  2. For an API solution

     file_url = 'https://someurl.com/text_file.txt'
     response = requests.get(file_url)
     if (response.status_code):
         data = response.text
     for line in enumerate(data.split('n')):
         print(line)
    
Answered By: Zeevi Gev
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.