-
파스쿠찌 매장정보 크롤링 (Beautifulsoup,지역별 열합계 구하기)DataAnalysis/웹크롤링 2022. 4. 19. 14:48
파스쿠찌 매장정보를 가져오는 전체코드
import requests from bs4 import BeautifulSoup import pandas as pd result = [] for pageNum in range(1,54): url = f'https://www.caffe-pascucci.co.kr/store/storeList.asp?page={pageNum}' #f string %로 만들어도 됨 response = requests.get(url) print('-------------------------- " ', pageNum) if response.status_code == 200: html = response.text soup = BeautifulSoup(html, 'html.parser') tbody = soup.find('tbody') tr_list = tbody.find_all('tr') for tr in tr_list: td_list=tr.find_all('td') store_num = td_list[0].text store_name= td_list[1].text #지점명 store_loc=td_list[2].text #지역 store_addr =td_list[4].find('p').text store_tel=td_list[5].find('span').text #전화번호 result.append([store_name,store_loc,store_addr,store_tel]) print(store_name,store_loc,store_addr,store_tel) else : print(response.status_code) pdResult = pd.DataFrame(result, columns=['store_name','store_loc','store_addr','store_tel']) pdResult.to_csv('pasccu_stores.csv', encoding='cp949') print(pdResult) print(len(pdResult)) seoul_count=0 busan_count=0 for row in pdResult['store_loc']: if row == '서울': seoul_count+=1 if row == '부산': busan_count+=1 print(seoul_count) acount=pdResult.groupby(['store_loc']).describe() print(acount.iloc[:,0:1])
열합계 구하는 방법 (매장별 갯수 가져오기)
1. for문을 이용하여 count를 세기
seoul_count=0 busan_count=0 for row in pdResult['store_loc']: if row == '서울': seoul_count+=1 if row == '부산': busan_count+=1 print(seoul_count)
2. groupby 집계함수 사용 : 데이터를 그룹별로 분류 가능 (column 속성 여러개 선택 가능)
acount=pdResult.groupby(['store_loc']).describe() print(acount.iloc[:,0:1])
'store_loc' 열에서 지역별로 데이터를 분류하여 나타낸다.
3. value_counts() 사용
print(pdResult['loc'].value_counts())
'DataAnalysis > 웹크롤링' 카테고리의 다른 글
[python] 코로나 홈페이지 웹 크롤링 및 데이터전처리 : 정적 페이지 웹크롤링, beautifulsoup 사용 (0) 2022.04.25 subway 서브웨이 전국매장 웹크롤링 (0) 2022.04.25 쿠우쿠우 웹크롤링 전국매장 정보 가져오기 (beautifulsoup) (0) 2022.04.22 카페베이 coffeebay 웹크롤링 매장정보 가져오기 (beautifulsoup) (0) 2022.04.22 Python 교촌치킨 국내매장 크롤링(Selenium 동적 웹크롤링) (3) 2022.04.17