Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 아마존해킹
- 비전공자개발정리
- 숫자구하기
- 한국디도스
- es6
- 퀵정렬
- es3
- 아키네이처
- 빗썸데이터
- 네트워크해킹
- 데이터분석
- 주식스팸
- 자바스크립트표준
- 브라우저 작동원리
- ECMA설명
- 1일 1로그 100일 완성 IT지식
- CS스터디
- es6문법
- 프로세서 속도와 심장 박동수
- 트위터해킹
- ES차이
- 알고리즘 문제 풀이
- HDD와 SSD의 차이
- 컴퓨터과학
- 공개API
- 컴퓨터 논리와 구조
- 이진검색
- 줌서비스
- ES5
- API요청
Archives
- Today
- Total
개발일지
웹개발 종합반 3주차 숙제 - 지니 뮤직 크롤링하기 본문
답안코드
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)
#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
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)
1. 크롤링 필요한 기본 코드,url 붙여넣기
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)
2. 지니뮤직 사이트 검사 > copy selector 가져오기
3. copy selector 코드 같은 부분 잘라내기
#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
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
nth-child(1)..(2) 는 각 부여된 번호로 하나만 가져올 수 없다. 다 가져올 수 있는건
#body-content > div.newest-list > div > table > tbody > tr
4. for문을 돌려 rank,title,artist를 가져옴
.text 문자를 가져옴 + [0:2]는 첫 문자부터 3 번째 문자 까지 가져옴
.strip() 공백 제거
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)
mongoDB 연결 해결을 아직 못함
4월 경 3주차 FAQ에 있는 몽고db certifi 패키지 설치로 해결
'강의 > 웹개발 종합반' 카테고리의 다른 글
웹개발 종합반 5주차 숙제 - 개발일지 쓰기 (0) | 2022.04.17 |
---|---|
웹개발 종합반 4주차 숙제 - 팬명록 완성 (0) | 2022.04.14 |
웹개발 종합반 2주차 숙제 - 날씨 API를 이용해서 현재기온 표시 (0) | 2022.04.10 |
웹개발 종합반 1주차 숙제 - 10cm 팬명록 만들기 (0) | 2022.04.08 |
Comments