1. 梗概:我是个化验师,我们可是也要我们做PPT,医学知识往往繁多复杂,要是有比较好的照片配合文字,可以大大增加PPT的效果。必应这个网站可以搜索到比较有价值的医学图片,要是你输入一个医学名词,同样的搜索引擎,必应检索到的医学图片比较精准。所以,今天,我们就来做这个批量下载必应图片的项目。

  1. 软件条件:安装anaconda的电脑。

  1. 需要写两个py:bing.py和downloader.py。

bing.py

from pathlib import Path
import urllib.request
import urllib
import imghdr
import posixpath
import reclass Bing:def __init__(self, query, limit, output_dir, adult, timeout,  filter='', verbose=True):self.download_count = 0self.query = queryself.output_dir = output_dirself.adult = adultself.filter = filterself.verbose = verboseself.seen = set()assert type(limit) == int, "limit must be integer"self.limit = limitassert type(timeout) == int, "timeout must be integer"self.timeout = timeout# self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0'}self.page_counter = 0self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 'AppleWebKit/537.11 (KHTML, like Gecko) ''Chrome/23.0.1271.64 Safari/537.11','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3','Accept-Encoding': 'none','Accept-Language': 'en-US,en;q=0.8','Connection': 'keep-alive'}def get_filter(self, shorthand):if shorthand == "line" or shorthand == "linedrawing":return "+filterui:photo-linedrawing"elif shorthand == "photo":return "+filterui:photo-photo"elif shorthand == "clipart":return "+filterui:photo-clipart"elif shorthand == "gif" or shorthand == "animatedgif":return "+filterui:photo-animatedgif"elif shorthand == "transparent":return "+filterui:photo-transparent"else:return ""def save_image(self, link, file_path):request = urllib.request.Request(link, None, self.headers)image = urllib.request.urlopen(request, timeout=self.timeout).read()if not imghdr.what(None, image):print('[Error]Invalid image, not saving {}\n'.format(link))raise ValueError('Invalid image, not saving {}\n'.format(link))with open(str(file_path), 'wb') as f:f.write(image)def download_image(self, link):self.download_count += 1# Get the image linktry:path = urllib.parse.urlsplit(link).pathfilename = posixpath.basename(path).split('?')[0]file_type = filename.split(".")[-1]if file_type.lower() not in ["jpe", "jpeg", "jfif", "exif", "tiff", "gif", "bmp", "png", "webp", "jpg"]:file_type = "jpg"if self.verbose:# Download the imageprint("[%] Downloading Image #{} from {}".format(self.download_count, link))self.save_image(link, self.output_dir.joinpath("Image_{}.{}".format(str(self.download_count), file_type)))if self.verbose:print("[%] File Downloaded !\n")except Exception as e:self.download_count -= 1print("[!] Issue getting: {}\n[!] Error:: {}".format(link, e))def run(self):while self.download_count < self.limit:if self.verbose:print('\n\n[!!]Indexing page: {}\n'.format(self.page_counter + 1))# Parse the page source and download picsrequest_url = 'https://www.bing.com/images/async?q=' + urllib.parse.quote_plus(self.query) \+ '&first=' + str(self.page_counter) + '&count=' + str(self.limit) \+ '&adlt=' + self.adult + '&qft=' + ('' if self.filter is None else self.get_filter(self.filter))request = urllib.request.Request(request_url, None, headers=self.headers)response = urllib.request.urlopen(request)html = response.read().decode('utf8')if html ==  "":print("[%] No more images are available")breaklinks = re.findall('murl&quot;:&quot;(.*?)&quot;', html)if self.verbose:print("[%] Indexed {} Images on Page {}.".format(len(links), self.page_counter + 1))print("\n===============================================\n")for link in links:if self.download_count < self.limit and link not in self.seen:self.seen.add(link)self.download_image(link)self.page_counter += 1print("\n\n[%] Done. Downloaded {} images.".format(self.download_count))

downloader.py

在最后一行代码download('urine crystal', output_dir="..\\Users\\cat", limit=100, timeout=1)的第一个参数修改你自己想要检索的英文关键字就可以了。

import os, sys
import shutil
from pathlib import Pathtry:from bing import Bing
except ImportError:  # Python 3from .bing import Bingdef download(query, limit=100, output_dir='dataset', adult_filter_off=True,
force_replace=False, timeout=60, filter="", verbose=True):# engine = 'bing'if adult_filter_off:adult = 'off'else:adult = 'on'image_dir = Path(output_dir).joinpath(query).absolute()if force_replace:if Path.is_dir(image_dir):shutil.rmtree(image_dir)# check directory and create if necessarytry:if not Path.is_dir(image_dir):Path.mkdir(image_dir, parents=True)except Exception as e:print('[Error]Failed to create directory.', e)sys.exit(1)print("[%] Downloading Images to {}".format(str(image_dir.absolute())))bing = Bing(query, limit, image_dir, adult, timeout, filter, verbose)bing.run()if __name__ == '__main__':download('urine crystal', output_dir="..\\Users\\cat", limit=100, timeout=1)#输入你想要搜索的英文关键字

效果:

尽管我们这个脚本可以下载到图片,但是,有时候图片下载不到,不过,下载不到的图片链接不会影响我们其他图片的下载,因为,我们有用try...except...

总结:总体这个脚本还可以用,不过,改进。

python批量下载必应图片相关推荐

  1. python批量下载必应每日壁纸

    文章目录 python批量下载必应每日壁纸 一.图片来源选择 二.python实现 python批量下载必应每日壁纸 必应搜索的每日背景壁纸都是高质量的图片,下载来当桌面壁纸再好不过了,微软官方也推出 ...

  2. python下载网页里面所有的图片-Python批量下载网页图片详细教程

    很多朋友在网上查找批量下载图片的方法~发觉挺凌乱的,无从下手.这里绿茶小编就来跟大家分享下使用Python批量下载图片方法. 目标:爬取某个网站上n多页的链接,每个链接有n多张图片,每一页对应一个文件 ...

  3. python批量下载网页文件-Python批量下载网页图片详细教程

    目标:爬取某个网站上n多页的链接,每个链接有n多张图片,每一页对应一个文件夹,每个文件夹包含n个链接所对应的文件夹. 步骤1:获得网页的所有链接,访问所有链接,获得链接里的图片地址. 步骤2:根据图片 ...

  4. python 批量下载网页图片_Python实现多线程批量下载图片

    <派森>(Python)3.13 win32 英文安装版 类型:编程工具大小:21M语言:英文 评分:8.7 标签: 立即下载 爬取图片可真的是一个可遇不可求的机会. 有需求就会动力. 目 ...

  5. python 批量下载网页图片_手把手教你爬取天堂网1920*1080大图片(批量下载)——实战篇|python基础教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ /1 前言/ 上篇文章 手把手教你爬取天堂网1920*1080大图片(批量下载)--理论篇我们谈及了天堂网 ...

  6. 用IDM 和 python 批量下载webp图片

    今天遇到了一个新的需求.网站上有许多webp图片,直接打开它,拒绝访问,浏览器打开也403,一看就是被保护了.写了python批量来下也失败.最后用IDM试一下,居然可以下载.但是一个一个下载太麻烦了 ...

  7. python批量下载txt图片批量导入到ppt

    初步有各个楼盘图片的相关网址,想批量导入到ppt,编相应序号,保证每一张一个楼盘,每页展示6-8张图片.第一步:根据相关网址批量下载图片 相关网址放入file_url_text.txt中,形成列表 ​ ...

  8. 用python批量下载网络图片大全_实战干货:用 Python 批量下载百度图片!

    为了做一个图像分类的小项目,需要制作自己的数据集.要想制作数据集,就得从网上下载大量的图片,再统一处理. 这时,一张张的保存下载,就显得很繁琐.那么,有没有一种方法可以把搜索到的图片直接下载到本地电脑 ...

  9. python批量下载网页图片及列表

    背景: 放假在家里没事鼓捣了一个用python实现的简单网页信息抓取的程序. demo功能: 将千图网的商业海报http://www.58pic.com/topic/419-1.html的(海报名字 ...

最新文章

  1. VCL 中的 Windows API 函数(6): BeginDeferWindowPos
  2. 这个逆袭的新同事,也太牛逼了
  3. Linux学习笔记4-三种不同类型的软件的安装(绿色软件、rpm软件、源代码软件)...
  4. Spring Boot 注解定时任务
  5. php清轩聚合登录平台网站源码
  6. 搭载高通骁龙855+UFS 3.0闪存 iQOO Neo 855版正式发布
  7. java method 注解_JAVA 注解详解及简单实例
  8. 一个CPU核可以设计为两个以上的线程
  9. mysql limit 算法_MYSQL分页limit速度太慢
  10. mysql怎么查询两页数据_mysql分页查询踩坑报告
  11. 电动汽车仿真系列-电动汽车复合电源的建模与仿真研究
  12. IIS 7 为 URL Rewrite 模块创建重写规则
  13. 自定义注解-用spel表达式 获取方法入参对象的的 get方法入参
  14. SLAM notes
  15. HDU-4622 Reincarnation (后缀自动机)
  16. Ubuntu下安装为知笔记
  17. android简易播放器2:activity和service同步显示
  18. 史上最全的NB-IoT知识,每个通信人都应该了解的
  19. git更换用户名和密码
  20. UI设计之【android 仿微信、QQ聊天,带表情,可翻页,带翻页拖动缓冲】

热门文章

  1. win10家庭中文版安装docker
  2. swoft框架使用笔记
  3. OKHttp3的使用和详解
  4. Linux查看实时网速
  5. 混凝土墙开洞_混凝土剪力墙开洞最好的方法
  6. 基于Python的Solidworks二次开发小尝试(二)
  7. 儿童python培训班哪个靠谱
  8. 最全的TV视频应用合集,包含50多款客户端,有丰富直播点播
  9. DriodDeveloper 技术干货大汇总
  10. LINUX CENTOS /VAR/LIB/DOCKER/CONTAINER目录导致系统存储爆满,占用大量存储解决方案