Skip to content

Instantly share code, notes, and snippets.

@youtube-jocoding
Created March 8, 2023 07:29
Show Gist options
  • Save youtube-jocoding/84c78a58b59a780fbf334f4b0977456e to your computer and use it in GitHub Desktop.
Save youtube-jocoding/84c78a58b59a780fbf334f4b0977456e to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import urllib.request
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome("C:/kefico/chromedriver.exe", options=options)
url = "https://pixabay.com/ko/images/search/"
keyword = input("검색어를 입력해주세요: ")
driver.get(url+keyword)
time.sleep(2)
imgTags = driver.find_elements(By.CSS_SELECTOR, ".photo-result-image")
imgTags = imgTags[0:10]
srcList = []
for tag in imgTags:
src = tag.get_attribute("src")
srcList.append(src)
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(srcList[0], "test.jpg")
@youtube-jocoding
Copy link
Author

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import urllib.request
import ssl
import random
ssl._create_default_https_context = ssl._create_unverified_context

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome("C:/kefico/chromedriver.exe", options=options)
url = "https://pixabay.com/ko/images/search/"
keyword = input("검색어를 입력해주세요: ")
driver.get(url+keyword)
time.sleep(2)
imgTags = driver.find_elements(By.CSS_SELECTOR, ".photo-result-image")
imgTags = imgTags[0:10]

srcList = []

for tag in imgTags:
    src = tag.get_attribute("src")
    srcList.append(src)

for i, src in enumerate(srcList):
    time.sleep(random.randint(1,3))
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36')]
    urllib.request.install_opener(opener)
    urllib.request.urlretrieve(src, f"img/{i}_{time.time()}.jpg")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment