스파르타 웹 프로그래밍 A-Z 기초 3주차 정리

SeonMan Kim·2021년 9월 17일
0

오늘까지 이런 식의 TIL을 작성하도록 하겠습니다.
이제 무언가 TIL을 바꾸자고 합니다.

pymongo 템플릿

# 저장 - 예시
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)

# 한 개 찾기 - 예시
user = db.users.find_one({'name':'bobby'})

# 여러개 찾기 - 예시 ( _id 값은 제외하고 출력)
same_ages = list(db.users.find({'age':21},{'_id':False}))

# 바꾸기 - 예시
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

# 지우기 - 예시
db.users.delete_one({'name':'bobby'})

네이버 영화 스트리핑

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://movie.naver.com/movie/sdb/rank/rmovie.nhn?sel=pnt&date=20200303', headers=headers)

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

# title = soup.select_one('#old_content > table > tbody > tr:nth-child(16) > td.title > div > a');
# print(title);
# print(title.text);
# print(title['href']);

trs = soup.select('#old_content > table > tbody > tr');
for tr in trs:
    rank_tag = tr.select_one('td:nth-child(1) > img');
    if rank_tag is not None:
        rank = rank_tag['alt'];
        title = tr.select_one('td.title > div > a').text;
        star = tr.select_one('td.point').text;

        doc = {
            'rank' : rank,
            'title' : title,
            'star' : star
        };

        db.movies.insert_one(doc);

지니뮤직 스크립핑

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:
    rank = tr.select_one('td.number').text.split(' ')[0].strip()
    title = tr.select_one('td.info > a.title.ellipsis').text.strip()
    singer = tr.select_one('td.info > a.artist.ellipsis').text.strip()
    print(rank, title, singer);
profile
안녕하세요

0개의 댓글