利用python爬虫爬取垃圾图片数据 您所在的位置:网站首页 垃圾桶垃圾图片 利用python爬虫爬取垃圾图片数据

利用python爬虫爬取垃圾图片数据

2024-07-10 01:44| 来源: 网络整理| 查看: 265

大家如果想构建相关垃圾相关的图片数据集,可以采用爬虫自行爬取和整理,网上随便找了个代码,该代码主要采用requests和BeautifulSoup函数进行爬取网页信息,下面给了每段函数给了注视(不会可以问Q525894654),可以改的地方给了注释和修改方法,再不会我也没有办法了。但是这个爬虫只是简单的爬取图片,可能爬到的图片还不够标准,不是很好的垃圾分类数据集,所以大家可以手动再次标注和筛选。

import re import requests from urllib import error from bs4 import BeautifulSoup import os num = 0 numPicture = 0 file = '' List = [] def Find(url):#该函数用于检测图片数量 global List print('正在检测图片总数,请稍等.....') t = 0 i = 1 s = 0 while t < 1000: Url = url + str(t) try: Result = requests.get(Url, timeout=7) except BaseException: t = t + 60 continue else: result = Result.text pic_url = re.findall('"objURL":"(.*?)",', result, re.S) # 先利用正则表达式找到图片url s += len(pic_url) if len(pic_url) == 0: break else: List.append(pic_url) t = t + 60 return s def recommend(url):#用于解析网页获取图片 Re = [] try: html = requests.get(url) except error.HTTPError as e: return else: html.encoding = 'utf-8' bsObj = BeautifulSoup(html.text, 'html.parser') div = bsObj.find('div', id='topRS') if div is not None: listA = div.findAll('a') for i in listA: if i is not None: Re.append(i.get_text()) return Re def dowmloadPicture(html, keyword):#下载图片 global num # t =0 pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # 先利用正则表达式找到图片url print('找到关键词:' + keyword + '的图片,即将开始下载图片...') for each in pic_url: print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each)) try: if each is not None: pic = requests.get(each, timeout=7) else: continue except BaseException: print('错误,当前图片无法下载') continue else: string = file + r'\\' + keyword + '_' + str(num) + '.jpg' fp = open(string, 'wb') fp.write(pic.content) fp.close() num += 1 if num >= numPicture: return if __name__ == '__main__': # 主函数入口 # tm = int(input('请输入每类图片的下载数量 ')) numPicture =10#大家可以修改这个参数进行修改自己想要获取图片的数量,当然可以通过定义使numPicture =tot,将Find函数检测到的图片数量全部爬下来 print('每类图片的下载数量 ',numPicture) line_list = [] with open('./name.txt') as file:#这一个自己建的text文件,放着自己要爬取垃圾的名称,比如废纸,烂苹果啥的.... line_list = [k.strip() for k in file.readlines()] # 用 strip()移除末尾的空格 for word in line_list: url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn=' tot = Find(url) Recommend = recommend(url) # 记录相关推荐 print('经过检测%s类图片共有%d张' % (word, tot)) file = word + '文件'#文件保存的地方 y = os.path.exists(file) if y == 1: print('该文件已存在,请重新输入') file = word + '文件夹2' os.mkdir(file) else: os.mkdir(file) t = 0 tmp = url while t < numPicture: try: url = tmp + str(t) result = requests.get(url, timeout=10) print(url) except error.HTTPError as e: print('网络错误,请调整网络后重试') t = t + 60 else: dowmloadPicture(result.text, word) t = t + 60 numPicture = numPicture + numPicture print('end')

爬取出来的结果:

 

比如爬的废纸(其中一个):



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有