python简单下载图片

以python运行次文件。输入要下载的关键字即可

#-*- coding:utf-8 -*-
import re
import requests



def dowmloadPic(html,keyword):
    with open('filename.html', 'w',encoding='utf-8') as f:
        f.write(html)
    print('write done')

    pic_url = re.findall('"objURL":"(.*?)",',html)
    i = 0
    print('找到关键词:'+keyword+'的图片,现在开始下载图片...')
    for each in pic_url:
        print('正在下载第'+str(i+1)+'张图片,图片地址:'+str(each))
        try:
            pic= requests.get(each, timeout=10)
        except requests.exceptions.ConnectionError:
            print('【错误】当前图片无法下载')
            continue
        string = 'pictures2\\'+keyword+'_'+str(i) + '.jpg'
        #resolve the problem of encode, make sure that chinese name could be store
        fp = open(string.encode('cp936'),'wb')
        fp.write(pic.content)
        fp.close()
        i += 1



if __name__ == '__main__':
    word = input("Input key word: ")
    url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&ct=201326592&v=flip'
    result = requests.get(url)
    dowmloadPic(result.text,word)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,791评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,732评论 19 139
  • 本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入...
    小小不懂11阅读 8,737评论 2 30
  • 文/Bruce.Liu1 1.Python前世今生 1.1.Python历史 Python的创始人: Guido ...
    BruceLiu1阅读 14,188评论 3 119
  • 明天就要上班了,开始自己日计划的历程。加一的微信群,大家从明天开始实施日计划,加一准备持续十个月的时间,每天...
    青苗妈2016阅读 1,332评论 0 1