这是一个简单的 Python 脚本,用于从百度图片搜索下载图片。

import requests
from bs4 import BeautifulSoup
import os

query = input('请输入要搜索的图片关键字:')

url = 'https://image.baidu.com/search/index?tn=baiduimage&word=' + query

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

if not os.path.exists(query):
    os.makedirs(query)

for a in soup.find_all('img', class_='main_img'):
    img_url = a['data-imgurl']
    filename = os.path.join(query, img_url.split('/')[-1])
    with open(filename, 'wb') as f:
        f.write(requests.get(img_url).content)

运行脚本后,程序会要求输入要搜索的图片关键字。然后,它会在当前目录下创建一个名为关键字的文件夹,并将搜索结果中的所有图片下载到该文件夹中。

Python 爬取百度图片脚本

原文地址: https://www.cveoy.top/t/topic/nO8T 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录