본문 바로가기
개발일지/selenium, BeautifulSoup, requests

파이썬 beautifulsoup, selenium (ft. 아직 안끝났지만 이미 어이가 없다;) - 진행중 (해결 x)

by 개발에정착하고싶다 2022. 6. 7.
320x100

파이썬 beautifulsoup, selenium 을 이용한 크롤링 과제 진행 중이다.

(해결 x)

 

다른것보다 어처구니가 없는건

분명 있는데 없다고 하는것과

없는데 작동이 되는거다.;;

 

오늘의 경우에는 명확히 없는데 작동되는 경우를 올려보고자 한다.

 

from bs4 import BeautifulSoup
from selenium import webdriver
import time

driver = webdriver.Chrome('../driver/chromedriver.exe')
driver.get('https://www.starbucks.co.kr/store/store_map.do')

loca_search = driver.find_element_by_class_name('loca_search')
loca_search.click()
time.sleep(2)

sido = driver.find_element_by_class_name('sido_arae_box')
li=sido.find_elements_by_tag_name('li') #elements는 해당하는 요소들을 모두 리스트로 반환
li[0].click() #서울은 리스트 첫번째에 위치하니 인덱스값 0으로 호출
time.sleep(2)

gugun = driver.find_element_by_class_name('gugun_arae_box')
li=gugun.find_elements_by_tag_name('li') #<li>태그 감싸고 있는 전체 서울의 구들 리스트로 반환
li[1].click()#인덱스로 강남구 접근
time.sleep(3)

source = driver.page_source

bs = BeautifulSoup(source, 'lxml')
branch = bs.find('ul', class_= 'quickSearchResultBoxSidoGugun')

stabucks_list_of_seoul = []

stabucks_finder = branch.find_all('li')
# for store in stabucks_finder:
#     stabucks_list_of_seoul.append(store)

# print(stabucks_list_of_seoul)

print("매장 수 : ", len(starbucksGangnam_list))
for stores in starbucksGangnam_list:
    print("매장명 :", stores.find('strong').text, "매장 주소 :" , stores.find('p').text)
    

# 코드 출처
# https://carriedata.tistory.com/entry/파이썬-크롤링-연습-1-매장찾기

 

문제의 코드는 가장 아래에서 starbucksGangnam_list

라고 표현된 부분이다.

나는 변수명도 변경해봤다.

결과적으로 "작동은"된다.

근데 내 코드 내에서는 starbucksGangnam_list 변수가 선언이 되지 않았음에도

작동이 된다;

즉, 반복문으로 한번 더 감싸주고 조정해 주어야하는데, 작동이 안된다.

 

아니 지금도 어이가 없네; 변수선언도 안되어있는게 어떻게 잘 작동이 되냐;

ㅋㅋㅋㅋㅋㅋ

300x250