Trying to download a file directly using Python and the Google Drive API exportlinks response.
Suppose I have an export link like this:
a) https://docs.google.com/feeds/download/documents/export/Export?id=xxxx&exportFormat=docx
To download this file, I simply paste it into the browser, and the file automatically downloads to my Downloads folder.
How do I do the same thing in Python?
EX: module.download_file_using_url(https://docs.google.com/feeds/download/documents/export/Export?id=xxxx&exportFormat=docx)
This is a repost of How do I download a file over HTTP using Python?
In Python 2, use urllib2 which comes with the standard library.
import urllib2 response = urllib2.urlopen('http://www.example.com/') html = response.read()
This is the most basic way to use the library, minus any error handling. You can also do more complex stuff such as changing headers. The documentation can be found here.
Related
I am trying to download data from UniProt using Python from within a script. If you follow the previous link, you will see a Download button, and then the option of choosing the format of the data. I would like to download the Excel format, compressed. Is there a way to do this within a script?
You can easily see the URL for that if you monitor it in the Firefox "netowork" tab or equivalent. For this page it seems to be https://www.uniprot.org/uniprot/?query=*&format=xlsx&force=true&columns=id,entry%20name,reviewed,protein%20names,genes,organism,length&fil=organism:%22Homo%20sapiens%20(Human)%20[9606]%22%20AND%20reviewed:yes&compress=yes. You should be able to download it using requests or any similar lib.
Example:
import requests
url = "https://www.uniprot.org/uniprot/?query=*&format=xlsx&force=true&columns=id,entry%20name,reviewed,protein%20names,genes,organism,length&fil=organism:%22Homo%20sapiens%20(Human)%20[9606]%22%20AND%20reviewed:yes&compress=yes"
with open("downloaded.xlsx.gz", "wb") as target:
target.write(requests.get(url).content)
Is this the correct code to run the search query and download the resulting csv file (you have to click Download CSV on the website to download the file). If not, how should i modify it and where should I look for the downloaded file? In the default Downloads folder or elsewhere?
import requests, csv, urllib
from urllib import request
URL = 'http://families.naeyc.org/programs/csv/CSV?attach=list'
response = urllib.request.urlopen(URL).read()
with open('file.csv','wb') as fx:
fx.write(response)
When you create a new file with open('file.csv','wb') it is created in the same folder as your Python script file
I am trying to write a code that will download all the data from a server which holds the .rar files about imaginary cadastrial particles for student projects. What I got for now is the query for the server which only needs to input a specific number of particle and access it as url to download the .rar file.
url = 'http://www.pg.geof.unizg.hr/geoserver/wfs?request=getfeature&version=1.0.0&service=wfs&&propertyname=broj,naziv_ko,kc_geom&outputformat=SHAPE-ZIP&typename=gf:katastarska_cestica&filter=<Filter+xmlns="http://www.opengis.net/ogc"><And><PropertyIsEqualTo><PropertyName>broj</PropertyName><Literal>1900/1</Literal></PropertyIsEqualTo><PropertyIsEqualTo><PropertyName>naziv_ko</PropertyName><Literal>Suma Striborova Stara (9997)</Literal></PropertyIsEqualTo></And></Filter>'
This is the "url" I want to open with the web browser module for a particle "1900/1" but this way I get an error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
When I manually input this url it downloads the file without a problem.
What is the way I can make this python web application work?
I used a webbrowser.open_new(url) option which does not work.
You're using the wrong tool. webbrowser is for controlling a native web browser. If you just want to download a file, use the requests module (or urllib.request if you can't install Requests).
import requests
r = requests.get('http://www.pg.geof.unizg.hr/geoserver/wfs', params={
'request': 'getfeature',
...
'filter': '<Filter xmlns=...>'
})
print(r.content) # or write it to a file, or whatever
Note requests will handle encoding GET parameters for you -- you don't need to worry about escaping the request yourself.
I am trying to download an excel file from a OneDrive location. My code works okay to get the file, but the file is corrupt (I get an error message):
import urllib2
data = urllib2.urlopen("enter url here")
with open('C:\\Video.xlsx', 'wb') as output:
output.write(data.read())
output.close()
print "done"
I use the guest access to the excel file so that I don't have to work with authentication. The resulting file seems to be 15KB, the original is 22KB.
I got it. The url has the format below:
'https://onedrive.live.com/view.aspx?cid=.....app=Excel'
So, all that I had to do was change "view" to "download" at that url, and used the code below:
import urllib.request
url = 'https://onedrive.live.com/view.aspx?cid=.....app=Excel'
urllib.request.urlretrieve(url, "test.xlsx")
You can't just download the Excel file directly from OneDrive using a URL. Even when you would share the file without any authorization, you'll probably still get a link to an intermediate HTML page, rather than the Excel binary itself.
To download items from your OneDrive, you'll first need to authenticate and then pass the location of the file you're after. You'll probably want to use the OneDrive REST API. The details on how to do that are documented on the OneDrive's SDK for Python GitHub page with some examples to get you started.
I'm using MultipartPostHandler in file sending. My code is following:
params = {'file':open(file_name, 'rb')}
headers = {'cookie':session_id}
urllib2.install_opener(urllib2.build_opener(MultipartPostHandler.MultipartPostHandler))
response = urllib2.urlopen(urllib2.Request("www.example.com/upload", params, headers))
How could I do the same (send file to the server) without using MultipartPostHandler? It would be good to use only buid-in python modules and urllib2. Is it possible.
MultipartPostHandler needs to install it using easy_install, pip or
from source. I want like to write the python script that would not
require new instalations.
Just add it to your original script - it is just one file. Copy paste the code for the module.
Unfortunately, there is no direct method available to post a multiple part file using urllib2. But there are ways to accomplish that by writing a custom form object using mimetype and mimetools module. You could follow this recipe and adopt your form to do a mutipart upload using urllib2.
(In Python3, urllib.request data can take a pointer to file object and that does read the whole file into memory)