오늘은 크롤링에 관해서 배웠다. 나도 처음 해보는거라 익숙하지 않아서 많이 해맸다. 하지만 하다보니 익숙해져서 재밌었다.
pip install selenium
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('URL')
time.sleep(3) #로드 되기전에 입력되지 않도록 방지
driver.close()
e = driver.fine_elements_by_class_name() #element로 하면 안찾아진다.
만약 주소 변경하고 싶으면...
driver.get('URL')
beautifulsoup를 설치해야함
pip install beautifulsoup4
from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from urllib.parse import quote_plus #한글 처리를 위해서
import time
# baseUrl = 'https://www.google.com/search?q='
baseUrl = 'https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query='
plusUrl = input('검색어를 입력하세요. : ')
url = baseUrl + quote_plus(plusUrl)#quote_plus 꼭 써야함
print(url)
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser') #html을 잘게잘라서 담겠다
f = open("a_text.txt", 'w')
# titleLists = soup.select('h3')
#
# for title in titleLists:
# data = title.text + "\n"
# f.write(data)
titleLists = soup.select('.api_txt_lines')
for title in titleLists:
print(title.text)
print(title.get('href'))
f.close()