Owe*_*wen 813 python http urllib
我有一个小工具,用于按计划从网站下载MP3,然后构建/更新播客XML文件,我显然已将其添加到iTunes.
创建/更新XML文件的文本处理是用Python编写的.我在Windows .bat
文件中使用wget 来下载实际的MP3.我宁愿用Python编写整个实用程序.
我努力寻找一种方法来实际下载Python中的文件,因此我采用了wget
.
那么,我如何使用Python下载文件?
Pab*_*loG 1066
还有一个,使用urlretrieve
:
import urllib
urllib.urlretrieve ("http://www.example.com/songs/mp3.mp3", "mp3.mp3")
Run Code Online (Sandbox Code Playgroud)
(对于Python 3+,使用'import urllib.request'和urllib.request.urlretrieve)
又一个,带有"进度条"
import urllib2
url = "http://download.thinkbroadband.com/10MB.zip"
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
Run Code Online (Sandbox Code Playgroud)
Cor*_*rey 437
在Python 2中,使用标准库附带的urllib2.
import urllib2
response = urllib2.urlopen('http://www.example.com/')
html = response.read()
Run Code Online (Sandbox Code Playgroud)
这是使用库的最基本方法,减去任何错误处理.您还可以执行更复杂的操作,例如更改标题.文档可以在这里找到.
hug*_*own 328
在2012年,使用python请求库
>>> import requests
>>>
>>> url = "http://download.thinkbroadband.com/10MB.zip"
>>> r = requests.get(url)
>>> print len(r.content)
10485760
Run Code Online (Sandbox Code Playgroud)
你可以跑去pip install requests
搞定.
请求与备选方案相比具有许多优点,因为API更简单.如果您必须进行身份验证,则尤其如此.在这种情况下,urllib和urllib2非常不直观和痛苦.
2015年12月30日
人们对进度条表示钦佩.这很酷,当然.现在有几种现成的解决方案,包括tqdm
:
from tqdm import tqdm
import requests
url = "http://download.thinkbroadband.com/10MB.zip"
response = requests.get(url, stream=True)
with open("10MB", "wb") as handle:
for data in tqdm(response.iter_content()):
handle.write(data)
Run Code Online (Sandbox Code Playgroud)
这基本上是30个月前@kvance所描述的实现.
Gra*_*ant 158
import urllib2
mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3")
with open('test.mp3','wb') as output:
output.write(mp3file.read())
Run Code Online (Sandbox Code Playgroud)
在wb
中open('test.mp3','wb')
打开一个文件(并清除所有现有文件)以二进制模式,所以你可以用它来代替刚才保存的文本数据.
bma*_*pin 110
import urllib.request
response = urllib.request.urlopen('http://www.example.com/')
html = response.read()
Run Code Online (Sandbox Code Playgroud)import urllib.request
urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3')
Run Code Online (Sandbox Code Playgroud)import urllib2
response = urllib2.urlopen('http://www.example.com/')
html = response.read()
Run Code Online (Sandbox Code Playgroud)import urllib
urllib.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3')
Run Code Online (Sandbox Code Playgroud)Sar*_*ana 27
使用wget模块:
import wget
wget.download('url')
Run Code Online (Sandbox Code Playgroud)
小智 21
Python 2/3的PabloG代码的改进版本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import ( division, absolute_import, print_function, unicode_literals )
import sys, os, tempfile, logging
if sys.version_info >= (3,):
import urllib.request as urllib2
import urllib.parse as urlparse
else:
import urllib2
import urlparse
def download_file(url, dest=None):
"""
Download and save a file specified by url to dest directory,
"""
u = urllib2.urlopen(url)
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
filename = os.path.basename(path)
if not filename:
filename = 'downloaded.file'
if dest:
filename = os.path.join(dest, filename)
with open(filename, 'wb') as f:
meta = u.info()
meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all
meta_length = meta_func("Content-Length")
file_size = None
if meta_length:
file_size = int(meta_length[0])
print("Downloading: {0} Bytes: {1}".format(url, file_size))
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = "{0:16}".format(file_size_dl)
if file_size:
status += " [{0:6.2f}%]".format(file_size_dl * 100 / file_size)
status += chr(13)
print(status, end="")
print()
return filename
if __name__ == "__main__": # Only run if this file is called directly
print("Testing with 10MB download")
url = "http://download.thinkbroadband.com/10MB.zip"
filename = download_file(url)
print(filename)
Run Code Online (Sandbox Code Playgroud)
Aki*_*kif 19
图书馆提供简单而Python 2 & Python 3
兼容的方式six
:
from six.moves import urllib
urllib.request.urlretrieve("http://www.example.com/songs/mp3.mp3", "mp3.mp3")
Run Code Online (Sandbox Code Playgroud)
H S*_*ooq 17
import os,requests
def download(url):
get_response = requests.get(url,stream=True)
file_name = url.split("/")[-1]
with open(file_name, 'wb') as f:
for chunk in get_response.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
download("https://example.com/example.jpg")
Run Code Online (Sandbox Code Playgroud)
akd*_*dom 14
我同意Corey,urllib2比urllib更完整,如果你想做更复杂的事情,应该是使用的模块,但为了使答案更加完整,如果你只想要基础知识,urllib是一个更简单的模块:
import urllib
response = urllib.urlopen('http://www.example.com/sound.mp3')
mp3 = response.read()
Run Code Online (Sandbox Code Playgroud)
会工作得很好.或者,如果您不想处理"响应"对象,可以直接调用read():
import urllib
mp3 = urllib.urlopen('http://www.example.com/sound.mp3').read()
Run Code Online (Sandbox Code Playgroud)
Jay*_*dev 14
以下是在python中下载文件最常用的调用:
urllib.urlretrieve ('url_to_file', file_name)
urllib2.urlopen('url_to_file')
requests.get(url)
wget.download('url', file_name)
注意:urlopen
并且urlretrieve
发现下载大文件(大小> 500 MB)时性能相对较差.requests.get
将文件存储在内存中,直到下载完成.
您可以使用 python请求
import os
import requests
outfile = os.path.join(SAVE_DIR, file_name)
response = requests.get(URL, stream=True)
with open(outfile,'wb') as output:
output.write(response.content)
Run Code Online (Sandbox Code Playgroud)
你可以使用shutil
import os
import requests
import shutil
outfile = os.path.join(SAVE_DIR, file_name)
response = requests.get(url, stream = True)
with open(outfile, 'wb') as f:
shutil.copyfileobj(response.content, f)
Run Code Online (Sandbox Code Playgroud)
小智 7
您也可以通过urlretrieve获取进度反馈:
def report(blocknr, blocksize, size):
current = blocknr*blocksize
sys.stdout.write("\r{0:.2f}%".format(100.0*current/size))
def downloadFile(url):
print "\n",url
fname = url.split('/')[-1]
print fname
urllib.urlretrieve(url, fname, report)
Run Code Online (Sandbox Code Playgroud)
如果安装了wget,则可以使用parallel_sync.
pip install parallel_sync
from parallel_sync import wget
urls = ['http://something.png', 'http://somthing.tar.gz', 'http://somthing.zip']
wget.download('/tmp', urls)
# or a single file:
wget.download('/tmp', urls[0], filenames='x.zip', extract=True)
Run Code Online (Sandbox Code Playgroud)
Doc:https: //pythonhosted.org/parallel_sync/pages/examples.html
这非常强大.它可以并行下载文件,在失败时重试,甚至可以在远程机器上下载文件.
在python3中,您可以使用urllib3和shutil libraires.使用pip或pip3下载它们(取决于python3是否默认)
pip3 install urllib3 shutil
Run Code Online (Sandbox Code Playgroud)
然后运行此代码
import urllib.request
import shutil
url = "http://www.somewebsite.com/something.pdf"
output_file = "save_this_name.pdf"
with urllib.request.urlopen(url) as response, open(output_file, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
Run Code Online (Sandbox Code Playgroud)
请注意,您下载urllib3
但urllib
在代码中使用
小智 6
5行内使用Python请求
import requests as req
remote_url = 'http://www.example.com/sound.mp3'
local_file_name = 'sound.mp3'
data = req.get(remote_url)
# Save file data to local copy
with open(local_file_name, 'wb')as file:
file.write(data.content)
Run Code Online (Sandbox Code Playgroud)
现在对远程文件的本地副本执行一些操作
我编写了以下代码,它可以在普通 Python 2 或 Python 3 中运行。
import sys
try:
import urllib.request
python3 = True
except ImportError:
import urllib2
python3 = False
def progress_callback_simple(downloaded,total):
sys.stdout.write(
"\r" +
(len(str(total))-len(str(downloaded)))*" " + str(downloaded) + "/%d"%total +
" [%3.2f%%]"%(100.0*float(downloaded)/float(total))
)
sys.stdout.flush()
def download(srcurl, dstfilepath, progress_callback=None, block_size=8192):
def _download_helper(response, out_file, file_size):
if progress_callback!=None: progress_callback(0,file_size)
if block_size == None:
buffer = response.read()
out_file.write(buffer)
if progress_callback!=None: progress_callback(file_size,file_size)
else:
file_size_dl = 0
while True:
buffer = response.read(block_size)
if not buffer: break
file_size_dl += len(buffer)
out_file.write(buffer)
if progress_callback!=None: progress_callback(file_size_dl,file_size)
with open(dstfilepath,"wb") as out_file:
if python3:
with urllib.request.urlopen(srcurl) as response:
file_size = int(response.getheader("Content-Length"))
_download_helper(response,out_file,file_size)
else:
response = urllib2.urlopen(srcurl)
meta = response.info()
file_size = int(meta.getheaders("Content-Length")[0])
_download_helper(response,out_file,file_size)
import traceback
try:
download(
"https://geometrian.com/data/programming/projects/glLib/glLib%20Reloaded%200.5.9/0.5.9.zip",
"output.zip",
progress_callback_simple
)
except:
traceback.print_exc()
input()
Run Code Online (Sandbox Code Playgroud)
笔记:
如果速度对你很重要,我对模块urllib
和进行了一个小的性能测试wget
,关于wget
我尝试了一次状态栏和一次没有。我拿了三个不同的 500MB 文件来测试(不同的文件 - 以消除在后台进行一些缓存的可能性)。在 debian 机器上测试,使用 python2。
首先,这些是结果(它们在不同的运行中是相似的):
$ python wget_test.py
urlretrive_test : starting
urlretrive_test : 6.56
==============
wget_no_bar_test : starting
wget_no_bar_test : 7.20
==============
wget_with_bar_test : starting
100% [......................................................................] 541335552 / 541335552
wget_with_bar_test : 50.49
==============
Run Code Online (Sandbox Code Playgroud)
我执行测试的方式是使用“配置文件”装饰器。这是完整的代码:
import wget
import urllib
import time
from functools import wraps
def profile(func):
@wraps(func)
def inner(*args):
print func.__name__, ": starting"
start = time.time()
ret = func(*args)
end = time.time()
print func.__name__, ": {:.2f}".format(end - start)
return ret
return inner
url1 = 'http://host.com/500a.iso'
url2 = 'http://host.com/500b.iso'
url3 = 'http://host.com/500c.iso'
def do_nothing(*args):
pass
@profile
def urlretrive_test(url):
return urllib.urlretrieve(url)
@profile
def wget_no_bar_test(url):
return wget.download(url, out='/tmp/', bar=do_nothing)
@profile
def wget_with_bar_test(url):
return wget.download(url, out='/tmp/')
urlretrive_test(url1)
print '=============='
time.sleep(1)
wget_no_bar_test(url2)
print '=============='
time.sleep(1)
wget_with_bar_test(url3)
print '=============='
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
urllib
似乎是最快的
仅出于完整性考虑,也可以使用该subprocess
软件包调用任何程序来检索文件。专用于检索文件的程序比Python函数(如)强大urlretrieve
。例如,wget
可以递归下载目录(-R
),可以处理FTP,重定向,HTTP代理,可以避免重新下载现有文件(-nc
),并且aria2
可以进行多连接下载,从而有可能加快下载速度。
import subprocess
subprocess.check_output(['wget', '-O', 'example_output_file.html', 'https://example.com'])
Run Code Online (Sandbox Code Playgroud)
在Jupyter Notebook中,还可以使用以下!
语法直接调用程序:
!wget -O example_output_file.html https://example.com
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1004084 次 |
最近记录: |