我正在尝试使用python的requests模块从Web下载并保存图像.
这是我使用的(工作)代码:
img = urllib2.urlopen(settings.STATICMAP_URL.format(**data))
with open(path, 'w') as f:
f.write(img.read())
Run Code Online (Sandbox Code Playgroud)
以下是使用以下内容的新(非工作)代码requests:
r = requests.get(settings.STATICMAP_URL.format(**data))
if r.status_code == 200:
img = r.raw.read()
with open(path, 'w') as f:
f.write(img)
Run Code Online (Sandbox Code Playgroud)
你能帮助我从响应中使用什么属性requests吗?
flush()不一定将文件的数据写入磁盘.使用flush()后跟os.fsync()来确保此行为.
所以我的问题是:Python到底在flush做什么?我认为它强制将数据写入磁盘,但现在我发现它没有.为什么?
我试图从网站下载PDF文件并将其保存到磁盘.我的尝试要么失败,要么编码错误,要么导致空白PDF.
In [1]: import requests
In [2]: url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'
In [3]: response = requests.get(url)
In [4]: with open('/tmp/metadata.pdf', 'wb') as f:
...: f.write(response.text)
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-4-4be915a4f032> in <module>()
1 with open('/tmp/metadata.pdf', 'wb') as f:
----> 2 f.write(response.text)
3
UnicodeEncodeError: 'ascii' codec can't encode characters in position 11-14: ordinal not in range(128)
In [5]: import codecs
In [6]: with codecs.open('/tmp/metadata.pdf', 'wb', encoding='utf8') as f:
...: f.write(response.text)
...:
Run Code Online (Sandbox Code Playgroud)
我知道这是某种编解码器问题,但我似乎无法让它工作.
我正在尝试创建一个与此wget命令完全相同的Python函数:
wget -c --read-timeout=5 --tries=0 "$URL"
Run Code Online (Sandbox Code Playgroud)
-c - 如果下载中断,请从中断处继续.
--read-timeout=5 - 如果超过5秒没有新数据进入,请放弃并重试.鉴于-c这意味着它将从它停止的地方再次尝试.
--tries=0 - 永远重试.
串联使用的这三个参数导致下载不会失败.
我想在我的Python脚本中复制这些功能,但我不知道从哪里开始......
我需要下载一个相当大的(~200MB)文件.我想出了如何在这里下载和保存文件.有一个进度条可以知道下载了多少,这将是一件好事.我找到了ProgressBar,但我不确定如何将两者合并在一起.
这是我尝试过的代码,但它没有用.
bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
with closing(download_file()) as r:
for i in range(20):
bar.update(i)
Run Code Online (Sandbox Code Playgroud) 我想使用python下载所有谷歌图像搜索图像.我使用的代码似乎有些问题.我的代码是
import os
import sys
import time
from urllib import FancyURLopener
import urllib2
import simplejson
# Define search term
searchTerm = "parrot"
# Replace spaces ' ' in search term for '%20' in order to comply with request
searchTerm = searchTerm.replace(' ','%20')
# Start FancyURLopener with defined version
class MyOpener(FancyURLopener):
version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'
myopener = MyOpener()
# Set count to 0
count= 0
for i in range(0,10):
# Notice that the …Run Code Online (Sandbox Code Playgroud) 我想通过HTTP协议下载文件urllib3.我已设法使用以下代码执行此操作:
url = 'http://url_to_a_file'
connection_pool = urllib3.PoolManager()
resp = connection_pool.request('GET',url )
f = open(filename, 'wb')
f.write(resp.data)
f.close()
resp.release_conn()
Run Code Online (Sandbox Code Playgroud)
但我想知道这样做的正确方法是什么.例如,它适用于大文件,如果没有做什么,使这个代码更容易容忍和可扩展.
注意.对我来说,重要的是不要使用urllib3库urllib2,因为我希望我的代码是线程安全的.
我使用Python Requests库下载一个大文件,例如:
r = requests.get("http://bigfile.com/bigfile.bin")
content = r.content
Run Code Online (Sandbox Code Playgroud)
大文件的下载速度为每秒+30 Kb,这有点慢.与bigfile服务器的每个连接都受到限制,所以我想建立多个连接.
有没有办法同时建立多个连接来下载一个文件?
我用selenium开始下载.下载完成后,需要采取某些措施,是否有任何简单的方法可以找出下载完成的时间?(我正在使用FireFox驱动程序)
我目前有我的代码的上传部分工作,我将如何将其转换为将从 box 文件夹中下载相应文件的程序?
这是上传程序:
import requests
import json
#the user acces token
access_token = 'UfUNeHhv4gIxFCn5WEXHgBJwfG8gHT2o'
#the name of the file as you want it to appear in box
dst_filename = 'box_file'
#the actual file path
src_directory = 'C:\Python\cache\\'
#the name of the file to be transferred
src_filename = 'Wildlife.wmv'
#the id of the folder you want to upload to
parent_id = '0'
counter = 1
for counter in range(1, 6):
src_file = (src_directory + src_filename + '-' + str(counter))
print(src_file) …Run Code Online (Sandbox Code Playgroud) python ×10
download ×2
box-api ×1
boxapiv2 ×1
fsync ×1
io ×1
networking ×1
python-2.7 ×1
python-3.x ×1
selenium ×1
urllib2 ×1
urllib3 ×1
web-scraping ×1
wget ×1