웹스크래핑이란 웹서버로 부터 html 전체를 가져와서 html 태그내에 있는 유용한 데이터를 추출하는 것이다.
최근 사이트들은 Scrapping 방지를 해 놔서 일반적인 python 의 requests.get(url) 로는 Timeout 에러가 발생한다.
하지만 웹 브라우저(Chrome, Firefox 등등)을 이용하면 제한은 없어지기 때문에 프로그램적으로 웹 브라우저를 통제하기 위해서 python에서 selenium 라이브러리를 통하여 작동할 OS에 설치되어 있는 브라우저에 맞는 Webdriver를 이용한다.

프로그램이 동작할 OS에서의 설정은 까다로운 편이지만, Google Colab 에서는 아래와 같이 간단하게 설정하면 된다.
|
!pip install google-colab-selenium
|
|
import google_colab_selenium as gs
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.add_argument("--headless")
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--no-sandbox")
options.add_argument("--lang=ko") # 언어별 설정
driver = gs.Chrome(options=options)
url = 'http://xxx.xxxs/xx/xxx/xxx/xxx001/?fw2=&srch_navi=1'
# 가져오기 driver.get(url)
# 페이지 소스 html = driver.page_source.encode('utf-8')
# 사용후엔 반드시 quit driver.quit()
# BeautifulSoup 으로 html tag 분석 soup = BeautifulSoup(html, 'html.parser')
body = soup.find("body")
|
'data science > python' 카테고리의 다른 글
| Dijkstra Algorithm가 구현된 osmnx 라이브러리를 이용한 경로 검색 (0) | 2025.02.22 |
|---|---|
| Dijkstra Algorithm (Google map 에서도 사용하는 경로 찾기) (0) | 2025.02.21 |
| dict 을 이용한 일괄 문자열 바꿔치기 (0) | 2025.02.12 |
| 여러 단어를 한 단어로 바꿔치기 (1) | 2025.02.11 |
| 반복되는 특정 단어를 복수 단어로 바꿔치기 (0) | 2025.02.11 |