热线电话:13121318867

登录
2019-06-27 阅读量: 818
利用python爬虫下载单个图片?

问、利用python爬虫下载单个图片?

答:

from hashlib import md5

import os

os.chdir('C:\\Users\\chen')

url='http://p3-tt.byteimg.com/large/pgc-image/fe1f67006cc14f88bcb2f616bb3419fb'

#保存图片

title=html['data'][1]['title']

resp = requests.get(url)#获取图片内容

img_path = 'img' + os.path.sep + title #os.path.sep,分隔符\\

os.makedirs(img_path)

file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format(

file_name=md5(resp.content).hexdigest(),

file_suffix='jpg')

with open(file_path, 'wb') as f:

f.write(resp.content)

0.0000
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子