경사도와 관련한 공공데이터들 파일 확장자가 shp등이어서, 자동 크롤링 코드를 짜는 것이 빠를 것 같아 제작하게 되었다.
아래의 사이트에서 크롤링 하려 한다. 크롤링은 법적 책임을 질 수 있기에 되도록이면 안 하고 싶지만 API 를 능수능란하게 사용하는 법을 알기 전까지는 수익성이 없을 때에 한하여 몇번 더 진행할 것 같다 ..
https://webgis.neins.go.kr/popup/searchGCadastralPopup.do
국토환경성평가지도
webgis.neins.go.kr
18번째 줄 k.send_keys 부분에 주소를 입력하면 해당 주소의 경사도가 크롤링된다.
지번 주소건 도로명 주소건 상관없다.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Chrome 브라우저 실행
driver = webdriver.Chrome()
try:
# 크롤링할 웹 페이지 URL
url = "https://webgis.neins.go.kr/popup/searchGCadastralPopup.do"
# 웹 페이지 열기
driver.get(url)
# 관악구 선택
k = driver.find_element(By.NAME, 'queryText')
k.send_keys('서울특별시 관악구 관악로 1 (신림동)')
k.send_keys("\n")
# 담기 버튼 클릭
g = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupRatepoint"]/div[2]/div[3]/ul/li/a'))
)
g.click()
# 분석 버튼 클릭
l = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupRatepoint"]/div[3]/a[1]'))
)
l.click()
# 팝업창 허용 - alert 다이얼로그 처리
alert = driver.switch_to.alert
alert.accept()
# 새 창이 나타날 때까지 기다림
new_window = WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
# 셀레늄이 바라보는 창 전환
driver.switch_to.window(driver.window_handles[1])
print('창 전환됨')
# 데이터 추출
percent5 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[2]/td[3]'))
)
print('5도 이하 :',percent5.text)
percent10 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[3]/td[3]'))
)
print('5도~10도 :',percent10.text)
percent15 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[4]/td[3]'))
)
print('10~15도 :',percent15.text)
percent20 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[5]/td[3]'))
)
print('15~20도 :',percent20.text)
percent25 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[6]/td[3]'))
)
print('20~25도 :',percent25.text)
percent30 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[7]/td[3]'))
)
print('25~30도 :',percent30.text)
percent30up = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="popupPointdetail"]/div[2]/div[3]/div/div[2]/div/table/tbody[1]/tr[7]/td[3]'))
)
print('30도 이상:',percent30up.text)
except Exception as e:
print("An error occurred:", str(e))
>>>
창 전환됨
5도 이하 : 11.23%
5도~10도 : 11.88%
10~15도 : 8.97%
15~20도 : 9.36%
20~25도 : 14.56%
25~30도 : 20.56%
30도 이상: 20.56%
참고문헌
셀레니움 오류 AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector(xpath, class_name, id, lin
1. 오류상황 셀레니움이 4.3.0 버전으로 업데이트 되면서 더이상 find_element_by_* 종류를 사용할 수 없게 되었다. 사용하려고하면 다음과 같은 오류 메시지를 뱉어버린다. AttributeError: 'WebDriver' object h
startcoding0.tistory.com
https://pythonblog.co.kr/coding/23/
find_element By 사용
셀레늄 By 는 소스를 보다 유연하고 유지 관리하기 쉽게 만드는 데 사용할 수 있습니다. 저도 By를 주로 사용합니다. 엘리먼트를 찾는 방법은 find element 사용하기- naver 와 동일합니
pythonblog.co.kr
https://ggondae.tistory.com/30
[selenium] Xpath 등을 이용하여 Element 동작 처리하기
from selenium import webdriver from selenium.webdriver.common.keys import Keys # 드라이버 생성 driver = webdriver.Chrome() # 네이버 이동 driver.get('https://www.naver.com') # 1. 네이버 첫 페이지에서 '카페'를 찾아 클릭 # '카페'
ggondae.tistory.com
[크롤링] 셀레니움에서 팝업창 해결하기 _ 파이썬, 2020년 내용
※ 본글에서는 셀레니움 자체에 대해서 다루지 않습니다. 오늘 드디어 셀레니움을 이용해서 자동으로 AP...
blog.naver.com
https://testmanager.tistory.com/126
Selenium WebDriver의 Alert(경고창) & Popup Window(팝업창) Handling(다루기)
알리미 란 무엇입니까?알림은 사용자에게 특정 정보를 제공하거나 특정 종류의 작업을 수행 할 수있는 권한을 요청하는 화면 알림을 표시하는 작은 메시지 상자입니다. 경고 목적으로도 사용될
testmanager.tistory.com
나중에 다시 참조할 것 같아서 ..
'Python' 카테고리의 다른 글
[교육데이터분석]관할행정구역 크롤링 (0) | 2024.06.09 |
---|---|
[Python] Pandas 인덱싱 하는 법 정리 (0) | 2023.08.11 |
[Python] Pandas에서 파일 데이터 조작 (0) | 2023.07.13 |
[Python] VS Code 인터프리터 바꾸는 법 (아나콘다가 VS code에서 자꾸 실행될 때) (0) | 2023.07.07 |
[Python] 모듈 datetime 및 pandas에서의 시계열 처리 (0) | 2023.07.05 |