일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- github
- 이클립스
- 안드로이드개발자모드
- 이클립스 폰트
- 오창 식당
- mongodb
- 아이폰
- pycharm
- openapi
- 이클립스 글씨체
- 내돈내산
- 오창 돈까스
- 스파르타코딩클럽
- 맥북단축키
- 오창맛집
- 코딩
- 중국노래
- ajax
- json
- 맥북
- 오창
- 이클립스 테마
- 개발
- 개발자옵션
- eclipse
- db데이터찾기
- 파이참
- git hub
- API
- 오창식당
Archives
- Today
- Total
나의 기록_나의 다이어리
[스파르타코딩클럽:웹개발종합반] 3주차 과제 (지니뮤직 1~50순위 곡 스크래핑해보기 - 순위, 제목, 가수) 본문
코딩/국비지원 개발인강 (스파르타코딩클럽)
[스파르타코딩클럽:웹개발종합반] 3주차 과제 (지니뮤직 1~50순위 곡 스크래핑해보기 - 순위, 제목, 가수)
NayDiary 2022. 11. 8. 17:02반응형
SMALL
1. PyCarm에서 Python 파일 새로 생성
2. 기본 세팅
- 지니 url
https://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20210701
- Pycharm에 입력 후 실행
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('https://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20210701',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
print(soup)
- 결과창에 무슨 정보가 뜨면서 '종료 코드 0(으)로 완료된 프로세스' 문구 확인
3. 지니 웹사이트에서 '순위 1'에 마우스 오른쪽버튼 → 검사 클릭
4. '순위'에 해당하는 코드 Copy → Copy selector
4. Pycharm에 복사 붙여넣기 → 순위 2도 똑같은 패턴으로 붙여넣기 → 코드가 어디까지 똑같은지 비교
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
#body-content > div.newest-list > div > table > tbody > tr:nth-child(2) > td.number
5. 코드 입력 후 PyCharm 실행
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('https://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20210701',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number
#body-content > div.newest-list > div > table > tbody > tr:nth-child(2) > td.number
# 'tr'까지 같은 부분 복사해서 soup.select로 묶어주기
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
# for문으로 목록에 있는 리스트 돌려주기
for tr in trs:
rank = tr.select_one('td.number')
#순위와 텍스트 표시
print(rank.text)
- 결과값이 나오긴 하나 깔끔하게 안 나옴
6. 결과값 깔끔하게 나오게 하기 (끊기)
- 0부터 2까지 끊어라
print(rank.text[0:2])
- 그랬더니 여백이 또 나옴
7. 결과값 여백 없애기 (strip)
- 양쪽 여백을 모두 없애라
print(rank.text[0:2].strip())
- 깔끔하게 순위 결과값이 나옴
- print() 안에 있던 '.text[0:2].strip()을 rank 줄 끝에 옮겨주고 print를 지워준다. (코드 깔끔하게 바꾸기)
rank = tr.select_one('td.number').text[0:2].strip()
8. 지니 웹사이트에 있는 제목도 검사→ Copy → Copy selector 해서 PyCarm에 붙여넣기
- 곡 제목의 Copy selector 값
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
- 겹치는 부분 빼고 td.info 부터만 붙여넣기 (soup으로 묶어준 부분 빼고)
title = tr.select_one('td.info > a.title.ellipsis')
8. 제목 표시하기 전체 코드
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('https://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20210701',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for tr in trs:
rank = tr.select_one('td.number').text[0:2].strip()
title = tr.select_one('td.info > a.title.ellipsis')
print(title.text.strip())
- 잘 나오는 것 확인 하였으니, artist 추가해야하니까 print 지워주고 title로 코드 옮겨서 깔끔하게 만들어주자
title = tr.select_one('td.info > a.title.ellipsis').text.strip()
9. 최종 완성
- 지니 웹사이트에서 가수(artist) 정보도 똑같이 Copy selector 후에 코드 완성해준다.
- 최종 완성 코드
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('https://www.genie.co.kr/chart/top200?ditc=M&rtm=N&ymd=20210701',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for tr in trs:
rank = tr.select_one('td.number').text[0:2].strip()
title = tr.select_one('td.info > a.title.ellipsis').text.strip()
artist = tr.select_one('td.info > a.artist.ellipsis').text
print(rank, title, artist)
- '순위, 제목, 가수' 모두 표시된다.
반응형
LIST
'코딩 > 국비지원 개발인강 (스파르타코딩클럽)' 카테고리의 다른 글
Comments