在本文中,我们将详细介绍如何以“更智能”的方式使用python下载文件?的各个方面,并为您提供关于python下载工具的相关解答,同时,我们也将为您带来关于python-3.x–如何使用Python访
在本文中,我们将详细介绍如何以“更智能”的方式使用python下载文件?的各个方面,并为您提供关于python下载工具的相关解答,同时,我们也将为您带来关于python-3.x – 如何使用Python访问存储桶GCS的子文件夹中的文件?、python下载大文件带进度条 python下载接口文件的进度条、Python下载文件、python下载文件demo的有用知识。
本文目录一览:- 如何以“更智能”的方式使用python下载文件?(python下载工具)
- python-3.x – 如何使用Python访问存储桶GCS的子文件夹中的文件?
- python下载大文件带进度条 python下载接口文件的进度条
- Python下载文件
- python下载文件demo
如何以“更智能”的方式使用python下载文件?(python下载工具)
我需要在Python中通过http下载多个文件。
最明显的方法就是使用urllib2:
import urllib2u = urllib2.urlopen(''http://server.com/file.html'')localFile = open(''file.html'', ''w'')localFile.write(u.read())localFile.close()
但我不得不面对以某种方式是讨厌的网址,这样说:http://server.com/!Run.aspx/someoddtext/somemore?id=121&m=pdf
。通过浏览器下载时,文件具有人类可读的名称,即。accounts.pdf
。
有什么办法可以在python中处理它,所以我不需要知道文件名并将其硬编码到脚本中?
答案1
小编典典像这样的下载脚本往往会推送一个标题,告诉用户代理该文件的名称:
Content-Disposition: attachment; filename="the filename.ext"
如果可以获取该标头,则可以获取正确的文件名。
还有另一个线程可以提供一些代码来进行Content-Disposition
抓取。
remotefile = urllib2.urlopen(''http://example.com/somefile.zip'')remotefile.info()[''Content-Disposition'']
python-3.x – 如何使用Python访问存储桶GCS的子文件夹中的文件?
from google.cloud import storage import os bucket = client.get_bucket('path to bucket')
上面的代码将我连接到我的桶,但我很难连接到桶中的特定文件夹.
我正在尝试此代码的变体,但没有运气:
blob = bucket.get_blob("training/bad") blob = bucket.get_blob("/training/bad") blob = bucket.get_blob("path to bucket/training/bad")
我希望能够访问坏子文件夹中的图像列表,但我似乎无法这样做.
尽管阅读了文档,我甚至都不完全理解blob是什么,而是基于教程来实现它.
谢谢.
解决方法
list_blobs()
函数指定前缀和分隔符参数
请参阅以下来自Google Listing Objects example(也是GitHub snippet)的示例
def list_blobs_with_prefix(bucket_name,prefix,delimiter=None): """Lists all the blobs in the bucket that begin with the prefix. This can be used to list all blobs in a "folder",e.g. "public/". The delimiter argument can be used to restrict the results to only the "files" in the given "folder". Without the delimiter,the entire tree under the prefix is returned. For example,given these blobs: /a/1.txt /a/b/2.txt If you just specify prefix = '/a',you'll get back: /a/1.txt /a/b/2.txt However,if you specify prefix='/a' and delimiter='/',you'll get back: /a/1.txt """ storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blobs = bucket.list_blobs(prefix=prefix,delimiter=delimiter) print('Blobs:') for blob in blobs: print(blob.name) if delimiter: print('Prefixes:') for prefix in blobs.prefixes: print(prefix)
python下载大文件带进度条 python下载接口文件的进度条
使用 python 下载大文件并显示进度条,可通过 requests 和 tqdm 模块实现。具体步骤为:1. 安装模块;2. 导入模块;3. 获取文件大小;4. 创建进度条;5. 下载文件并更新进度条。下载完成后,进度条自动关闭。
如何使用 Python 下载大文件并显示进度条
开门见山回答:
使用 Python 下载大文件并显示进度条,可以使用 requests 模块搭配 tqdm 模块实现。
详细步骤:
立即学习“Python免费学习笔记(深入)”;
1. 安装必要的模块
pip install requests tqdm
2. 导入模块
import requests from tqdm import tqdm
3. 获取文件大小
url = "https://example.com/large_file.zip" response = requests.head(url) file_size = int(response.headers.get(''Content-Length'', 0))
4. 创建进度条
pbar = tqdm(total=file_size, unit=''B'', unit_scale=True)
5. 下载文件并更新进度条
with requests.get(url, stream=True) as r: for chunk in r.iter_content(chunk_size=1024): if chunk: pbar.update(len(chunk)) pbar.write(f''Downloaded {pbar.n/file_size*100:.2f}%'')
当下载完成时,进度条将自动关闭。
代码示例:
import requests from tqdm import tqdm url = "https://example.com/large_file.zip" response = requests.head(url) file_size = int(response.headers.get(''Content-Length'', 0)) pbar = tqdm(total=file_size, unit=''B'', unit_scale=True) with requests.get(url, stream=True) as r: for chunk in r.iter_content(chunk_size=1024): if chunk: pbar.update(len(chunk)) pbar.write(f''Downloaded {pbar.n/file_size*100:.2f}%'')
以上就是
Python下载文件
import os
import sys
import socket
import urllib
import urllib2
def Schedule(nBlock, nBlkSize, nFileSize):
''''''''''
nBlock:block num
nBlkSize:block size
nFileSize:remote size
''''''
if nFileSize != -1:
per = 100.0 * nBlock * nBlkSize / nFileSize
if per > 100 :
per = 100
os.write(1, ''\r processing percent: %.2f%%'' % per)
else:
os.write(1, ''\r processing download: %.2f MBytes'' % ((nBlock * nBlkSize) / (1024 * 1024)))
sys.stdout.flush()
def getRemoteFileSize(url, proxy=None):
"""
"""
opener = urllib2.build_opener()
if proxy:
if url.lower().startswith(''https://''):
opener.add_handler(urllib2.ProxyHandler({''https'' : proxy}))
else:
opener.add_handler(urllib2.ProxyHandler({''http'' : proxy}))
request = urllib2.Request(url)
request.get_method = lambda: ''HEAD''
try:
response = opener.open(request)
response.read()
except Exception, e:
print ''%s %s'' % (url, e)
else:
return dict(response.headers).get(''content-length'', 0)
def downloadRemoteFile(strUrl, strLocalFile):
"""
"""
socket.setdefaulttimeout(10)
print "Beg Download %s -> %s" % (strUrl, strLocalFile)
urllib.urlretrieve(strUrl, strLocalFile, Schedule)
print "\nEnd Download %s -> %s\n" % (strUrl, strLocalFile)
if __name__ == ''__main__'':
url1=''http://www.site-digger.com/uploads/soft/130313/China_Regions.csv''
url2=''http://a.hiphotos.baidu.com/image/w%3D2048/sign=ca64d241e9c4b7453494b016fbc41f17/1c950a7b02087bf4cae00381f0d3572c11dfcf4e.jpg''
url3=''http://dldir1.qq.com/music/clntupate/QQMusic_Setup_1020.exe''
url3=''http://news.qq.com''
downloadRemoteFile(url1, "1.csv")
downloadRemoteFile(url2, "1.jpg")
downloadRemoteFile(url3, "1.html")
这个帖子貌似写的比较详细,读者可以参考:
http://blog.ihipop.info/2010/10/1736.html
python下载文件demo
from urllib.request import urlretrieve
from urllib.request import urlopen
from bs4 import BeautifulSoup
#下载文件demo
html = urlopen("http://www.pythonscraping.com")
bsObj = BeautifulSoup(html,"html.parser")
imageLocation = bsObj.find("a",{"id":"logo"}).find("img")["src"]
urlretrieve(imageLocation,"logo.jpg")
今天关于如何以“更智能”的方式使用python下载文件?和python下载工具的分享就到这里,希望大家有所收获,若想了解更多关于python-3.x – 如何使用Python访问存储桶GCS的子文件夹中的文件?、python下载大文件带进度条 python下载接口文件的进度条、Python下载文件、python下载文件demo等相关知识,可以在本站进行查询。
本文标签: