资源简介

python爬取360搜索引擎图片,用于物体识别和样本训练 自定义函数def getBaiduImag(category,length,path),用于采集360图片,三个参数分别为搜索的“关键词”category,采集的图片数量length,保存图片的路径path。 自动检索当前下载目录下已有的图片数量,在此基础上再次采集length张图片。 需要导入的库文件有import requests、import json、import urllib、import os

资源截图

代码片段和文件信息

import requests
import json
import urllib
import pypinyin
import os



# 不带声调的(style=pypinyin.NORMAL)
def pinyin(word):
    s = ‘‘
    for i in pypinyin.pinyin(word style=pypinyin.NORMAL):
        s += ‘‘.join(i)
    return s



Collect_Picture_category = ‘苹果‘
Collect_Picture_length = 100
Collect_Picture_SavePath = ‘D:/test/1/‘
Collect_Picture_Source_Index = 2
Collect_Picture_Source = pinyin(‘360图片‘)

def CurrentPicture_list():
    filename_number = []
    Collect_Pic_path = Collect_Picture_SavePath[0:len(Collect_Picture_SavePath) - 1]
    for files in os.listdir(Collect_Pic_path):
        if files.endswith(“.jpg“):  # 填写规则
            (filename extension) = os.path.splitext(files)
            if filename[0:len(Collect_Picture_Source + ‘_‘ + pinyin(
                    Collect_Picture_category) + ‘_‘)] == Collect_Picture_Source + ‘_‘ + pinyin(
                Collect_Picture_category) + ‘_‘:
                filename_number.append(
                    filename[len(Collect_Picture_Source + ‘_‘ + pinyin(Collect_Picture_category) + ‘_‘):])
    return filename_number

#从百度图片搜索结果中抓取图片,相当于在搜索框中直接搜索
def get360Imag(Collect_Picture_categoryCollect_Picture_lengthCollect_Picture_SavePath):
    if not os.path.exists(Collect_Picture_SavePath):
        os.mkdir(Collect_Picture_SavePath)
    try:
        #最后面的参数pn代表从pn开始抓取,rn为抓取的图片数量
        if len(CurrentPicture_list()) != 0:
            start_Collect_Index = int(max(CurrentPicture_list())) + 1
        else:
            start_Collect_Index = 0
        print(‘start_Collect_Index:‘ + str(start_Collect_Index))
        Current_Collect_Length = Collect_Picture_length
        n = 0
        Each_start_Index = start_Collect_Index
        for x in range(10000):
            print(‘****************‘)
            print(‘x:‘ + str(x))
            print(‘Each_start_Index:‘+str(Each_start_Index))
     

评论

共有 条评论