Beautiful Soup

Doya·2025년 4월 9일

ESTSOFT_AI개발7기

목록 보기
30/43

개요

  • Beautiful Soup를 이용한 웹 크로링

Beautiful Soup

  • HTML과 XML 문서들의 구문을 분석하기 위한 파이썬 패키지
  • HTML로부터 데이터를 추출하기 위해 사요할 수 있는 파싱된 페이지의 파스 트리를 만듬
  • 웹 스크래핑에 유용함

참고 사이트
https://www.crummy.com/software/BeautifulSoup/bs4/doc

실습

  • 카페베네 사이트에서 매장찾기를 이용하여 매장명 및 매장 주소 얻기
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options)

def CoffeeBean_store(result):
    CoffeeBean_URL = "https://www.coffeebeankorea.com/store/store.asp"
    driver = webdriver.Chrome() 
             
    for i in range(1, 50):  #마지막 매장번호(최근 신규 매장번호 for i in range(1, 389): 
        driver.get(CoffeeBean_URL)
        time.sleep(1)  #웹페이지 연결할 동안 1초 대기
        try:
            driver.execute_script("storePop2(%d)" %i)
            time.sleep(1) # 1초 대기
            html = driver.page_source
            soupCB = BeautifulSoup(html, 'html.parser')
            store_name_h2 = soupCB.select("div.store_txt > h2")
            print( 'store_name_h2결과' , store_name_h2 )
            store_name = store_name_h2[0].string
            print(store_name)  #매장 이름 출력하기
            store_info = soupCB.select("div.store_txt > table.store_table > tbody > tr > td") 
            store_address_list = list(store_info[2])
            print('store_address_list 결과 ',store_address_list)
            store_address = store_address_list[0]
            store_phone = store_info[3].string
            result.append([store_name]+[store_address]+[store_phone])
        except:
            continue 
    return
    
def main():
    result = []
    CoffeeBean_store(result) 
    bean_tbl = pd.DataFrame(result, columns=('store', 'address','phone'))
    bean_tbl.to_csv('./data/CoffeeBean.csv', encoding='cp949', mode='w', index=False)
 
 
main()  
  • 실행 결과는 올리지 않을 예정
profile
안녕하세요. 도야입니다

0개의 댓글