/open_chromedriver.py Secret
Last active
October 21, 2021 17:08
Star
You must be signed in to star a gist
크롤링 연습 with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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