执行下载图片的时候出现问题
for header in headers.items():
AttributeError: 'set' object has no attribute 'items'

for header in headers.items():
AttributeError: 'set' object has no attribute 'items'

2017-07-13
res = s.get('https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png')
for header in res.headers.items():
print header
下载图片完整代码为:
def download_image_improved():
'''
demo:下载图片,会自动关闭流
:return:
'''
# 伪造Header 信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36'}
# 限定Url
url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"
from contextlib import closing
with closing(requests.get(url, headers=headers, stream=True)) as response:
# 打开文件
with open('testfile.jpg', 'wb') as fd:
# 每128k写入文件
for chunk in response.iter_content(128):
fd.write(chunk)举报