“ 지연되는 프로젝트에 인력을 더 투입하면 오히려 더 늦어진다. ”
- Frederick Philips Brooks
Mythical Man-Month 저자
728x90
반응형
0.바탕화면에 'python' 폴더 만들기
1. vscode 실행
2. 브라우저에서 python 다운로드(최신버전)
https://www.python.org/downloads/
3. vscode에 python 폴더 열기
4. vs에서 python 폴더 안에 starbucks.py 파일 만들기 (python > starbucks.py)
5. vs에서 terminal열기
6. terminal 에서 selenium 설치하기
pip install selenium
7. terminal에서 설치프로그램 확인하기
pip list
8. terminal에 beautifulSoup4설치하기
pip install beautifulsoup4
9. starbucks.py 파일에 선생님이 준 코드(스타벅스 위치 검색 프로그램) 붙여넣기
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup as bs
from datetime import datetime
import time
import json
import re
browser = webdriver.Chrome()
browser.get("https://www.starbucks.co.kr/store/store_map.do")
time.sleep(10)
# 지역검색 클릭
localSearch = browser.find_element(By.CSS_SELECTOR, "#container > div > form > fieldset > div > section > article.find_store_cont > article > header.loca_search > h3 > a")
localSearch.click()
time.sleep(5)
# 시/도 클릭
si = browser.find_element(By.CSS_SELECTOR, ".loca_step1_cont .sido_arae_box li:nth-child(1)")
si.click()
time.sleep(5)
# 서울 전체를 클릭
gu = browser.find_element(By.CSS_SELECTOR, "#mCSB_2_container > ul > li:nth-child(1) > a")
gu.click()
time.sleep(5)
# html 파일 받아오기
html_source = browser.page_source
soup = bs(html_source, 'lxml')
titles = soup.select(".quickSearchResultBoxSidoGugun .quickResultLstCon strong")
addresses = soup.select(".quickSearchResultBoxSidoGugun .quickResultLstCon .result_details")
stores_data = []
for title, address in zip(titles, addresses):
clean_title = title.text.strip() # 제목의 불필요한 공백 제거
clean_address = re.sub(r'\d{4}-\d{4}', '', address.text).strip() # 전화번호 패턴을 찾아 제거
stores_data.append({"title": clean_title, "address": clean_address})
# 오늘 날짜 가져오기
today = datetime.now().strftime('%Y.%m.%d')
# 매장의 전체 개수
total_count = len(stores_data)
# json 파일 추가하기
filename = f'starbucks_stores_seoul_{today}_{total_count}.json'
with open(filename, 'w', encoding='utf-8') as f:
json.dump(stores_data, f, ensure_ascii=False, indent=4)
print(f'JSON 파일 저장 완료: {filename}')
10. terminal에서 python starbucks.py실행
11. chromedrive 다운로드
-https://chromedriver.chromium.org/downloads
12. 내 pc에 맞는 크롬드라이버를 다운로드해서 starbucks.py가 있는 폴더에 chromedriver.exe를 넣어줌
13. 버전체크 chrome://version/
14. https://www.youtube.com/watch?v=yQ20jZwDjTE&t=2751s
유튜브로 스크랩핑 공부하기