웹개발 3주차

고영승·2022년 1월 21일
0
post-thumbnail

몽고디비 데이터베이스에 값을 저장하고 불러오는 것도 해보고 파이썬을 가지고 크롤링도 해보았다.
전체적으로 처음배우는 것이라 이해가 잘 안돼고 연속해서 듣지 못하고 띄엄띄엄 들었더니 맨날 복습 하면서 다시 기억을 되새기는 것을 반복했다. 웹개발 어렵다..

import requests
from bs4 import BeautifulSoup

from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.dbsparta

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=D&ymd=20200403&hh=23&rtm=N&pg=1',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

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

for tr in trs:
title = tr.select_one('td.info > a.title.ellipsis').text.strip()
rank = tr.select_one('td.number').text[0:2].strip()
singer = tr.select_one('td.info > a.artist.ellipsis').text
print(rank, title, singer)

profile
코린이

0개의 댓글