Python3实现批量下载百度搜索图片

版权声明:本文为博主原创文章,未经博主允许不得转载(pan_jinquan) https://blog.csdn.net/guyuealian/article/details/78732282

 Python3实现批量下载百度搜索图片

目录

 Python3实现批量下载百度搜索图片

1.通过关键字下载百度搜索的图片

2.根据链接URL下载图片


1.通过关键字下载百度搜索的图片

     在别人的代码基础做了修改,增加了关键字word定义,下载图片数量的imageNum设置,以及图像保存路径和名字的设置,需要requests模块的支持,直接使用pip安装即可,命令:pip install requests

# -*- coding: utf-8 -*-

import json
import itertools
import urllib
import requests
import os
import re
import sys
print("hah")
#############################################################
word="眼镜"                 #图片搜索的关键字,目前仅支持单个关键词
imageNum =10;               #下载图片的数目
saveImagePath="glasses3"    #保存图片的途径
indexOffset=0               #图像命名起始点
#############################################################

str_table = {
    '_z2C$q': ':',
    '_z&e3B': '.',
    'AzdH3F': '/'
}

char_table = {
    'w': 'a',
    'k': 'b',
    'v': 'c',
    '1': 'd',
    'j': 'e',
    'u': 'f',
    '2': 'g',
    'i': 'h',
    't': 'i',
    '3': 'j',
    'h': 'k',
    's': 'l',
    '4': 'm',
    'g': 'n',
    '5': 'o',
    'r': 'p',
    'q': 'q',
    '6': 'r',
    'f': 's',
    'p': 't',
    '7': 'u',
    'e': 'v',
    'o': 'w',
    '8': '1',
    'd': '2',
    'n': '3',
    '9': '4',
    'c': '5',
    'm': '6',
    '0': '7',
    'b': '8',
    'l': '9',
    'a': '0'
}

# str 的translate方法需要用单个字符的十进制unicode编码作为key
# value 中的数字会被当成十进制unicode编码转换成字符
# 也可以直接用字符串作为value
char_table = {ord(key): ord(value) for key, value in char_table.items()}

# 解码图片URL
def decode(url):
    # 先替换字符串
    for key, value in str_table.items():
        url = url.replace(key, value)
    # 再替换剩下的字符
    return url.translate(char_table)

# 生成网址列表
def buildUrls(word):
    word = urllib.parse.quote(word)
    url = r"http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord={word}&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word={word}&face=0&istype=2nc=1&pn={pn}&rn=60"
    urls = (url.format(word=word, pn=x) for x in itertools.count(start=0, step=60))
    return urls

# 解析JSON获取图片URL
re_url = re.compile(r'"objURL":"(.*?)"')
def resolveImgUrl(html):
    imgUrls = [decode(x) for x in re_url.findall(html)]
    return imgUrls

def downImg(imgUrl, dirpath, imgName):
    filename = os.path.join(dirpath, imgName)
    try:
        res = requests.get(imgUrl, timeout=15)
        if str(res.status_code)[0] == "4":
            print(str(res.status_code), ":" , imgUrl)
            return False
    except Exception as e:
        print("抛出异常:", imgUrl)
        print(e)
        return False
    with open(filename, "wb") as f:
        f.write(res.content)
    return True


def mkDir(dirName):
    dirpath = os.path.join(sys.path[0], dirName)
    if not os.path.exists(dirpath):
        os.mkdir(dirpath)
    return dirpath

if __name__ == '__main__':
    print("=" * 50)
   # word = input("请输入你要下载的图片关键词:\n")

    dirpath = mkDir(saveImagePath)

    urls = buildUrls(word)
    index = 0
    for url in urls:
        print("正在请求:", url)
        html = requests.get(url, timeout=10).content.decode('utf-8')
        imgUrls = resolveImgUrl(html)
        if len(imgUrls) == 0:  # 没有图片则结束
            break
        for url in imgUrls:
            if downImg(url, dirpath, str(index+indexOffset) + ".jpg"):
                index += 1
                print("正在下载第 %s 张" % (index+indexOffset))
                if index==imageNum:
                    break
        if index==imageNum:
            print("---------------------下载完成----------------------")
            print("下载结果保存在脚本目录下的文件夹中,文件名字:" + saveImagePath)
            break

 


2.根据链接URL下载图片

     如果已知图片的URL地址,可直接通过下面的方法下载

# -*-coding: utf-8 -*-
"""
    @Project: dataset
    @File   : download_url_image.py
    @Author : panjq
    @E-mail : pan_jinquan@163.com
    @Date   : 2018-07-12 10:00:19
"""

import requests
import os

def downImg(imgUrl, dirpath, imgName):
    filename = os.path.join(dirpath, imgName)
    try:
        res = requests.get(imgUrl, timeout=15)
        if str(res.status_code)[0] == "4":
            print(str(res.status_code), ":" , imgUrl)
            return False
    except Exception as e:
        print("抛出异常:", imgUrl)
        print(e)
        return False
    with open(filename, "wb") as f:
        f.write(res.content)
    return True

#读取txt数据函数
def download_url_image(fileName,out_dir):
    '''
    读取txt中URL下载链接,并下载到指定文件中
    :param fileName: 保存URl的TXT文件
    :param out_dir:  下载图片的保存目录
    :return: None
    '''

    Data=[]
    with open(fileName) as txtData:
        lines=txtData.readlines()
        for line in lines:
            try:
                lineData=line.strip()#去除空白和逗号“,”
                str_list = lineData.split(';')
                image_url=str_list[0]
                image_name=image_url.split('/')[-1]
                downImg(image_url,out_dir,image_name)
                print(image_url)
            except:
                print("下载失败:",image_url)
                continue

if __name__ == '__main__':
    fileName='/home/ubuntu/project/tfTest/image_list.txt'
    out_dir="/home/ubuntu/project/tfTest/dataset"
    download_url_image(fileName,out_dir)

 其中image_list.txtb保存URL的地址:

https://farm3.staticflickr.com/2334/1828778894_271415878a_o.jpg;4;369
https://farm4.staticflickr.com/3149/2355285447_290193393a_o.jpg;0;295
https://farm3.staticflickr.com/2090/1792526652_8f37410561_o.jpg;3;2332
https://farm3.staticflickr.com/2099/1791684639_044827f860_o.jpg;7;5920
 

   资料参考:http://blog.csdn.net/guyuealian/article/details/78730200

阅读更多

扫码向博主提问

pan_jinquan

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 图像处理
  • AI人工智能
  • OpenCV
  • TensorFlow
  • Caffe
去开通我的Chat快问
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页