Skip to content

Instantly share code, notes, and snippets.

@Moong-bee
Last active October 21, 2021 17:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
크롤링 연습 with Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
### Chrom Setting
chrome_driver_dir = "/Users/moongbee/Downloads/chromedriver"
chrome_option = Options()
chrome_option.add_experimental_option('debuggerAddress', 'localhost:9222')
driver = webdriver.Chrome(chrome_driver_dir, options=chrome_option)
### Open Goggle
driver.get("https://google.com")
### Search Cat
search_input = driver.find_element_by_css_selector('input.gLFyf.gsfi')
search_input.send_keys('고양이')
search_input.send_keys(Keys.ENTER)
### Goto Image
image_tab = driver.find_element_by_xpath('//*[@id="hdtb-msb"]/div[1]/div/div[2]/a')
image_tab.click()
### Get First Image
image = driver.find_element_by_xpath('//*[@id="islrg"]/div[1]/div[1]/a[1]/div[1]/img')
### Print Image src attribute
print(image.get_attribute('src'))
from urllib.request import urlretrieve
### Download Image
urlretrieve(image.get_attribute('src'), "[파일 저장할 경로]/[파일명.확장자]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment