0
Can a file be downloaded from url using python
i want to download a ile from url
7 Respuestas
+ 4
Why python when you can use your browser?
+ 3
or maybe he wants to code a simple downloader with python
+ 2
because he wants the user to input something in python and get the info he needs from the browser maybe?
+ 2
import urllib.request
urllib.request.urlretrieve('http://example.com/big.zip', 'file/on/disk.zip')
(See http://stackoverflow.com/questions/20714910/urllib-request-for-JUMP_LINK__&&__python__&&__JUMP_LINK-3-3-not-working-to-download-file)
+ 2
#Try this code .
# python geturl.py http://example.com/downloads/bigfile.zip
# from http://code.activestate.com/recipes/576530-download-a-url-with-a-console-progress-meter/
import os, sys, urllib
def _reporthook(numblocks, blocksize, filesize, url=None):
#print ("reporthook(%s, %s, %s)" % (numblocks, blocksize, filesize))
base = os.path.basename(url)
#XXX Should handle possible filesize=-1.
try:
percent = min((numblocks*blocksize*100)/filesize, 100)
except:
percent = 100
if numblocks != 0:
sys.stdout.write("\b"*70)
sys.stdout.write("%-66s%3d%%" % (base, percent))
def geturl(url, dst):
print ("get url '%s' to '%s'" % (url, dst))
if sys.stdout.isatty():
urllib.urlretrieve(url, dst, lambda nb, bs, fs, url=url: _reporthook(nb,bs,fs,url))
sys.stdout.write('\n')
else: urllib.urlretrieve(url, dst)
if __name__ == "__main__":
if len(sys.argv) == 2:
url = sys.argv[1]
base = url[url.rindex('/')+1:]
geturl(url, base)
elif len(sys.argv) == 3:
url, base = sys.argv[1:]
geturl(url, base)
else:
print ("Usage: geturl.py URL [DEST]")
sys.exit(1)
+ 2
# This one may be useful too
#http://sametmax.com/peut-on-compiler-un-programme-JUMP_LINK__&&__python__&&__JUMP_LINK/
import requests
import docopt
u
title = parse(p).getroot().find('head').find('title').text
print('Downloaded "%s"' % title)
0
可以