본문 바로가기

Python

Python] 크롤링한 데이터 DB에 업데이트하기

https://www.genie.co.kr/chart/top200

 

지니차트>실시간 - 지니

AI기반 감성 음악 추천

www.genie.co.kr

사이트에 있는

1~50 까지의 순위, 곡명, 가수 데이터 크롤링하기


크롤링 기본 코드

import requests
from bs4 import BeautifulSoup


header = {'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'}
a = requests.get('https://www.genie.co.kr/chart/top200',headers=header)
b = BeautifulSoup(a.text, 'html.parser')

 

위처럼 순위, 곡명, 가수 세 데이터 코드 복사

#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis
#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(1) > td.info > a.artist.ellipsis

코드 부분 나누기

 

같은 코드

#body-content > div.newest-list > div > table > tbody 

달라지는 데이터

 tr:nth-child(1)

다른 코드

td.info > a.title.ellipsis
td.number
td.info > a.artist.ellipsis

 

은 코드 변수에 저장

data = b.select('#body-content > div.newest-list > div > table > tbody > tr')

1~50 까지의 순위를 알기 위해 for 문 작성

for music in data:

각 변수에 코드의 나머지부분 저장

title = music.select_one(' td.info > a.title.ellipsis').text
rank = music.select_one(' td.number').text
name = music.select_one(' td.info > a.artist.ellipsis').text

 


실행시켜보기 

print(rank,title,name)

순위 변동과 공백 때문에 위처럼 출력

 


순위 변동과 공백 삭제하기

title = music.select_one(' td.info > a.title.ellipsis').text.strip()
rank = music.select_one(' td.number').text[0:2].strip()
name = music.select_one(' td.info > a.artist.ellipsis').text

순위 부분의 택스트 범위 조절, strip으로 공백 삭제

 

출력 확인


크롤링한 데이터 DB에 업데이트하기

 

pymongo 사용하기위한 코드 넣기

from pymongo import MongoClient
client = MongoClient('여기에 URL 입력')
db = client.dbsparta

for문 안에 데이터 업데이트 코드 작성

doc = {"rank": rank,
       'title': title,
       'name': name}
db.music.insert_one(doc)

DB 업데이트 확인

 

 


전체 코드

import requests
from bs4 import BeautifulSoup

from pymongo import MongoClient
client = MongoClient('url입력')
db = client.dbsparta


header = {'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'}
a = requests.get('https://www.genie.co.kr/chart/top200',headers=header)
b = BeautifulSoup(a.text, 'html.parser')

data = b.select('#body-content > div.newest-list > div > table > tbody > tr')

for music in data:

    title = music.select_one(' td.info > a.title.ellipsis').text.strip()
    rank = music.select_one(' td.number').text[0:2].strip()
    name = music.select_one(' td.info > a.artist.ellipsis').text

    doc = {"rank": rank,
           'title': title,
           'name': name}
    db.music.insert_one(doc)

'Python' 카테고리의 다른 글

Python] API 만들고 사용하기  (0) 2022.11.03
Python] Flask  (0) 2022.11.03
Python] DB연결, 조작 (mongoDB 사용)  (0) 2022.11.02
Python] 크롤링 - bs4 (Pycharm)  (0) 2022.11.02
Python] Requests 패키지 사용하기 (pycharm)  (0) 2022.11.02


Calendar
«   2025/03   »
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
더보기
Archives
Visits
Today
Yesterday