python批量下载必应图片

  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 re



class Bing:
    def __init__(self, query, limit, output_dir, adult, timeout,  filter='', verbose=True):
        self.download_count = 0
        self.query = query
        self.output_dir = output_dir
        self.adult = adult
        self.filter = filter
        self.verbose = verbose
        self.seen = set()

        assert type(limit) == int, "limit must be integer"
        self.limit = limit
        assert 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 = 0
        self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 
      'AppleWebKit/537.11 (KHTML, like Gecko) '
      'Ch
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python 3来批量下载Bing搜索的图片,可以按照以下步骤进行: 1. 首先,需要安装requests和beautifulsoup4这两个库。可以使用以下命令安装它们: ``` pip install requests pip install beautifulsoup4 ``` 2. 导入所需的模块: ```python import requests from bs4 import BeautifulSoup import os ``` 3. 创建一个函数来下载图片: ```python def download_image(url, save_dir): filename = os.path.join(save_dir, url.split('/')[-1]) response = requests.get(url, stream=True) if response.status_code == 200: with open(filename, 'wb') as file: file.write(response.content) print('成功下载图片:', filename) else: print('无法下载图片:', url) ``` 4. 定义一个函数来搜索并批量下载图片: ```python def download_images(query, save_dir, num_images): search_url = 'https://www.bing.com/images/search?q=' + query response = requests.get(search_url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') image_tags = soup.find_all('img') count = 0 for image_tag in image_tags: image_url = image_tag['src'] if image_url.startswith('https://'): download_image(image_url, save_dir) count += 1 if count == num_images: break else: print('搜索失败') ``` 5. 调用download_images函数并指定搜索的关键字、保存目录以及要下载图片数量: ```python query = '美食' # 搜索关键字 save_dir = 'images' # 图片保存目录 num_images = 10 # 要下载图片数量 download_images(query, save_dir, num_images) ``` 运行脚本后,将会在指定目录下下载指定数量的Bing搜索结果中的图片。 需要注意的是,根据Bing的使用条款,禁止使用非官方API进行批量下载图片。因此,在使用此方式之前,请确保你已阅读并遵守相关使用条款。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值