4

I'm completely new to Python, I want to download a file by sending a request to the server. When I type it into my browser, I see the CSV file is downloaded, but when I try sending a get request it does not return anything. for example:

import urllib2
response = urllib2.urlopen('https://publicwww.com/websites/%22google.com%22/?export=csv')
data = response.read()
print 'data: ',  data

It does not show anything, how can I handle that? When I search on the web, all the questions are about how to send a get request. I can send the get request, but I have no idea of how the file can be downloaded as it is not in the response of the request.

I do not have any idea of how to find a solution for that.

halfer
  • 18,701
  • 13
  • 79
  • 158
Alex
  • 1,498
  • 3
  • 20
  • 37
  • 1
    The question is already answered. https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python – Pratik Shivarkar Mar 05 '18 at 15:30
  • The post referenced as answering this questions was asked/answered in AUG 2008 and uses an API from the urllib.request that is described by the documentation as "Legacy Interface" and mentions that the interface might become deprecated at some point in the future. Surely there a better way exists that isn't 10 years old? I've seen suggestions to use requests with stream=True, but also complaints that it's slower than curl by quite a bit. https://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py#comment77074794_39217788 – gskluzacek Oct 20 '18 at 03:11

3 Answers3

8

You can use the urlretrieve to download the file

EX:

u = "https://publicwww.com/websites/%22google.com%22/?export=csv"

import urllib
urllib.request.urlretrieve (u, "Ktest.csv")
Peter
  • 1,104
  • 10
  • 10
Rakesh
  • 75,210
  • 17
  • 57
  • 95
8

You can also download a file using requests module in python.

import shutil

import requests

url = "https://publicwww.com/websites/%22google.com%22/?export=csv"
response = requests.get(url, stream=True)
with open('file.csv', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)
del response
bigbounty
  • 13,123
  • 4
  • 20
  • 50
2
import os
os.system("wget https://publicwww.com/websites/%22google.com%22/?export=csv")

You could try wget, if you have it.

whackamadoodle3000
  • 6,185
  • 4
  • 21
  • 38
  • 3
    Please don't down-vote without providing a reason, i see two down-votes and not a single reason for it. – Ubdus Samad Mar 05 '18 at 15:33
  • I downvote this because the question asks how to do it in Python. Calling out to the os and running another executable is not "in Python". Have a look at @Rakesh's answer to see how it's done using the urllib library. – jschreiner Feb 12 '20 at 21:40
  • This doesn't work on Windows. – Louis Yang Dec 24 '20 at 22:24