언어/python
selenium
파아랑새
2018. 7. 3. 15:35
from selenium import webdriver
from bs4 import BeautifulSoup
import time
#_________________________________________
#driver = webdriver.PhantomJS('C:/Users/sleep/Desktop/driver/phantomjs-2.1.1-windows/bin/phantomjs.exe')
driver = webdriver.Chrome('C:/Users/sleep/Desktop/driver/chromedriver.exe')
# 암묵적으로 웹 자원 로드를위해 3초까지 기다려 준다.
driver.implicitly_wait(3)
# url에 접근
driver.get('https://search.naver.com/search.naver?where=image&sm=tab_jum&query=')
driver.find_element_by_name('query').send_keys("곰")
driver.find_element_by_xpath('//*[@id="nx_search_form"]/fieldset/button').click()
#html = driver.page_source
driver.execute_script("window.scrollTo(50, document.body.scrollHeight);")
time.sleep(3)
print (driver.execute_script("return document.body.scrollHeight"))
html = driver.page_source
# _______ 웹 데이터 추출
#print (html)
#_sau_imageTab > div.photowall._photoGridWrapper > div:nth-child(5) > div:nth-child(13) > a.thumb._thumb > img
soup = BeautifulSoup(html, "html.parser")
query = '#_sau_imageTab > div.photowall._photoGridWrapper > div > div:nth-of-type(99) > a.thumb._thumb > img'
notice = soup.select(query)
for i in notice:
print (i)
# last_height = driver.execute_script("return document.body.scrollHeight")
# print (last_height)
driver.close()