Skip to content

Instantly share code, notes, and snippets.

@taoka-toshiaki
Created August 6, 2022 08:49
Show Gist options
  • Save taoka-toshiaki/1cd532737236695640ea4660a6539e30 to your computer and use it in GitHub Desktop.
Save taoka-toshiaki/1cd532737236695640ea4660a6539e30 to your computer and use it in GitHub Desktop.
bingサイトで画像ダウンロードするために作られたものです。
from functools import cache
import time
import requests
import os
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = Options()
# download_path = 'C:\python\images\face'
# options.add_experimental_option("prefs", {"download.default_directory": download_path})
# driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.bing.com/images/search?q=顔&form=HDRSC2&first=1&tsc=ImageHoverTitle")
driver.set_window_size(945, 1012)
time.sleep(3)
sl = 700
for i in range(30):
driver.execute_script("window.scrollTo(0," + str(sl) + ")")
time.sleep(3)
sl = sl + 700
img = []
for x in range(10):
for y in range(100):
try:
txt = driver.find_element(By.XPATH,"//*[@id=\"mmComponent_images_2\"]/ul[" + str(int(y +1 )) +"]/li[" + str(int(x +1 )) +"]/div/div[1]/a").get_attribute("m")
hoge = str(txt).split(",")
#print(hoge)
img.append([s for s in hoge if "murl" in s])
except:
print("errors not image")
driver.quit()
file_dir = "C:\\python\\images\\face\\"
for imgdata in img:
url = str(imgdata).split(":")[1] + ":" + str(imgdata).split(":")[2]
url = url.replace('"',"").replace("']","")
print(url)
try:
urlData = requests.get(url).content
with open(os.path.join(file_dir,os.path.basename(url)),'wb') as f:
f.write(urlData)
except:
print("errors not Download")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment