동아리, 스터디, 교육/멋쟁이 사자처럼
[멋사 요일별 스터디] 배스킨라빈스 아이스크림 순위 크롤링 하기
pxatd
2022. 5. 14. 21:12
728x90
사용한 사이트 url : http://www.baskinrobbins.co.kr/menu/list.php?top=A
[배스킨라빈스]
행복을 전하는 프리미엄 아이스크림, 배스킨라빈스 공식 홈페이지 입니다.
www.baskinrobbins.co.kr
위 사이트에서 n위 : 아이스크림 이름의 태그를 찾아 크롤링 하는게 과제이다.
1.
from bs4 import BeautifulSoup
import requests
url = 'http://www.baskinrobbins.co.kr/menu/list.php?top=A'
res = requests.get(url)
soup=BeautifulSoup(res.text,"html.parser")
icecreams = soup.find_all('span','ice_name')
for i in range(len(icecreams)):
print(f'{i+1}위: {icecreams[i].get_text()}')
이 방법은 스터디 운영진 팀에서 알려준 정답 코드! 코드가 복잡하지는 않지만 이번에 새롭게 배운 f 출력법을 사용해볼 수 있어서 흥미로웠다.
2.
pip install bs4
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get("http://www.baskinrobbins.co.kr/menu/list.php?top=A",headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select("#content > section")
for tr in trs:
title_a = tr.select_one("#slider > div > ul")
if title_a is not None:
print(title_a.text,end='\n')
이건 내가 구글링을 통해 크롤링에 대해 공부한 뒤 작성한 코드
크롤링은 정말.. 코드를 어떻게 짜냐에 따라 무궁무진 한 것 같다.
근데 내 코드로 작성하면 이렇게 n위 사이에 줄바꿈이 세 칸씩 적용되는데 왜 이런지 모르겠다. 없애보려고 end="" 코드도 넣어줬는데 소용없었다 . . . 더 생각해봐야지
아무래도 if title_a is not None: 부분 때문에 그런거같다.
728x90