python 爬取百度网页图片

点击运行后:输入我们要搜的关键词

注意!!!!!!

self.directory  需要修改为自己保存的本地地址,本地地址  \{}  不要动 ,其它的就不需要修改了,我们搜索关键词是什么,那么我们的文件夹就是什么名字。

spider.json_count = 10   # 定义下载10组图像,也就是三百张

不需要这么多图片就把spider.json_count基数调小,看自己需求

import requests
import json
from urllib import parse
import os
import time

class BaiduImageSpider(object):
    def __init__(self):
        self.json_count = 0  # 请求到的json文件数量(一个json文件包含30个图像文件)
        self.url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=5179920884740494226&ipn=rj&ct=201326592&is=&fp=result&queryWord={}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word={}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&nojc=&pn={}&rn=30&gsm=1e&1635054081427='
        self.directory = r"C:\Users\Lenovo\Desktop\{}"  # 存储目录  这里需要修改为自己希望保存的目录  {}不要丢
        self.header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30'
        }

    # 创建存储文件夹
    def create_directory(self, name):
        self.directory = self.directory.format(name)
        # 如果目录不存在则创建
        if not os.path.exists(self.directory):
            os.makedirs(self.directory)
        self.directory += r'\{}'

    # 获取图像链接
    def get_image_link(self, url):
        list_image_link = []
        try:
            response = requests.get(url, headers=self.header, timeout=10)  # Get方式获取网页数据
            response.raise_for_status()  # 如果响应状态码不是200,则抛出异常
            json_info = response.json()
            for index in range(30):
                list_image_link.append(json_info['data'][index]['thumbURL'])
        except (requests.RequestException, ValueError, KeyError) as e:
            print(f"Error occurred while fetching or parsing the JSON data: {e}")
        return list_image_link

    # 下载图片
    def save_image(self, img_link, filename):
        try:
            response = requests.get(img_link, headers=self.header, timeout=10)
            response.raise_for_status()  # 如果响应状态码不是200,则抛出异常
            with open(filename, "wb") as f:
                f.write(response.content)
                print("正在爬取: " + filename)
        except requests.RequestException as e:
            print(f"Error occurred while downloading the image: {e}")

    # 入口函数
    def run(self):
        search_name = input("请输入你要搜索的关键词: ")
        search_name_parse = parse.quote(search_name)  # 编码

        self.create_directory(search_name)

        pic_number = 0  # 图像数量
        for index in range(self.json_count):
            pn = (index + 1) * 30
            request_url = self.url.format(search_name_parse, search_name_parse, str(pn))
            list_image_link = self.get_image_link(request_url)
            for link in list_image_link:
                pic_number += 1
                self.save_image(link, self.directory.format(str(pic_number) + '.jpg'))
                time.sleep(0.2)  # 休眠0.2秒,防止封ip
        print(search_name + " image download completed.")

if __name__ == '__main__':
    spider = BaiduImageSpider()
    spider.json_count = 10   # 定义下载10组图像,也就是三百张
    spider.run()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值