2018年8月

来自最佳答案(推荐下面第一个):
You can either use the response.raw file object, or iterate over the response.

To use the response.raw file-like object will not, by default, decode compressed responses (with GZIP or deflate). You can force it to decompress for you anyway by setting the decode_content attribute to True (requests sets it to False to control decoding itself). You can then use shutil.copyfileobj() to have Python stream the data to a file object:

import requests
import shutil

r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
    with open(path, 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj(r.raw, f)       

To iterate over the response use a loop; iterating like this ensures that data is decompressed by this stage:

r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
    with open(path, 'wb') as f:
        for chunk in r:
            f.write(chunk)

This'll read the data in 128 byte chunks; if you feel another chunk size works better, use the Response.iter_content() method with a custom chunk size:

r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
    with open(path, 'wb') as f:
        for chunk in r.iter_content(1024):
            f.write(chunk)

Note that you need to open the destination file in binary mode to ensure python doesn't try and translate newlines for you. We also set stream=True so that requests doesn't download the whole image into memory first.

官方文档的解释:
Existence Check

The { item : { $exists: false } } query matches documents that do not contain the item field:

db.inventory.find( { item : { $exists: false } } )

Studio3T里的Query查询条件可以是 {downloaded:{$exists: false}} 即可以查出那些没有这个downloaded字段的数据

魔术方法调用方式解释
__new__(cls [,...])instance = MyClass(arg1, arg2)new 在创建实例的时候被调用
__init__(self [,...])instance = MyClass(arg1, arg2)init 在创建实例的时候被调用
__cmp__(self, other)self == other, self > other, 等。在比较的时候调用
__pos__(self)+self一元加运算符
__neg__(self)-self一元减运算符
__invert__(self)~self取反运算符
__index__(self)x[self]对象被作为索引使用的时候
__nonzero__(self)bool(self)对象的布尔值
__getattr__(self, name)self.name # name 不存在访问一个不存在的属性时
__setattr__(self, name, val)self.name = val对一个属性赋值时
__delattr__(self, name)del self.name删除一个属性时
__getattribute(self, name)self.name访问任何属性时
__getitem__(self, key)self[key]使用索引访问元素时
__setitem__(self, key, val)self[key] = val对某个索引值赋值时
__delitem__(self, key)del self[key]删除某个索引值时
__iter__(self)for x in self迭代时
__contains__(self, value)value in self, value not in self使用 in 操作测试关系时
__concat__(self, value)self + other连接两个对象时
__call__(self [,...])self(args)“调用”对象时
__enter__(self)with self as x:with 语句环境管理
__exit__(self, exc, val, trace)with self as x:with 语句环境管理
__getstate__(self)pickle.dump(pkl_file, self)序列化
__setstate__(self)data = pickle.load(pkl_file)序列化