# coding=utf8
import os
import time
import requests
import json
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
from bs4 import BeautifulSoup
def download_images(keyword, num_images):
url = f"https://www.bing.com/images/search?q={keyword}"
driver = webdriver.Chrome() # or webdriver.Chrome(), depending on your browser
driver.get(url)
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
# Scroll down several times to load more images
for _ in range(10):
try:
time.sleep(2) # wait for 2 seconds
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1) # pause between scrolls
except WebDriverException:
continue
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
img_tags = soup.find_all("a", {"class": "iusc"})
os.makedirs(keyword, exist_ok=True)
count = 0
for i, img in enumerate(img_tags):
try:
img_info = json.loads(img["m"])
img_url = img_info["murl"]
response = requests.get(img_url, stream=True)
if response.status_code == 200:
with open(f"{keyword}/{keyword}_{i}.jpg", "wb") as out_file:
out_file.write(response.content)
count += 1
if count >= num_images:
break
except:
pass
driver.quit()
download_images("关键词", 20)
通过图片搜索引擎自动下载图片
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1 Project Overview 1.1 Project Description Based on tenso...
- 1 Project Overview 1.1 Project Description Based on tenso...
- 内容较多、请先马后看;借助es分布式计算的能力,使得早期易企秀APP端图片搜索功能就具备了高可用、可扩展的能力 1...