์ง„๋„ํ‘œ
01 - 02 ๋ณต์Šต
03 - 06 ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๊ณต๋ถ€
07 - 08 ํŒŒ์ด์ฌ_ํฌ๋กค๋ง
09 - 14 mongoDB


1 ~ 2. ํŒŒ์ด์ฌ, gitbash ์„ค์น˜ํ•˜๊ธฐ & 2์ฃผ์ฐจ ๋ณต์Šต

๋กœ๋”ฉํ›„ ๋ฐ”๋กœ ํ˜ธ์ถœํ•˜๊ธฐ

$(document).ready(function(){
listing();
});

function listing() {
console.log('ํ™”๋ฉด ๋กœ๋”ฉ ํ›„ ์ž˜ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค');
}
//๋ณ„ ์ถœ๋ ฅ. ์ˆซ์ž ์ž…๋ ฅ->๋ณ„ ๊ฐœ์ˆ˜๋Œ€๋กœ ๋‚˜์˜ค๋„๋ก
let star_image = 'โญ'.repeat(star)

์ดˆ๋ฐ˜์— ํ•ด๋†“์€ ์นด๋“œ๋ฐ•์Šค๋Š” empty()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ€๋ ค์ฃผ๊ฑฐ๋‚˜ ์ง€์šฐ๊ธฐ
$('#cards-box').empty()


 //๋กœ๋”ฉํ›„ ๋ฐ”๋กœ ์‹คํ–‰
        $(document).ready(function () {
            listing();
        });

        function listing() {
            $('#cards-box').empty()
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/web/api/movie",
                data: {},
                success: function (response) {
                    let rows = response['movies']
                    //์™œ for๋ฌธ์œผ๋กœ ๋„๋Š” ๊ฑธ๊นŒ?
                    for (let i = 0; i < rows.length; i++){
                        let title = rows[i]['title']
                        let desc = rows[i]['desc']
                        let image = rows[i]['image']
                        let star = rows[i]['star']
                        let comment = rows[i]['comment']

                        //๋ณ„ ์ถœ๋ ฅ. ์ˆซ์ž ์ž…๋ ฅ->๋ณ„ ๊ฐœ์ˆ˜๋Œ€๋กœ ๋‚˜์˜ค๋„๋ก
                        let star_image = 'โญ'.repeat(star)

                        let temp_html = `<div class="col">
                                            <div class="card">
                                                <img src="${image}"
                                                     class="card-img-top" alt="...">
                                                <div class="card-body">
                                                    <h5 class="card-title">${title}</h5>
                                                    <p class="card-text">${desc}</p>
                                                    <p>${star_image}</p>
                                                    <p class="mycomment">${comment}</p>
                                                </div>
                                            </div>
                                        </div>`


                        $('#cards-box').append(temp_html)
                    }

                }
            })
        }

3 ~ 6. ํŒŒ์ด์ฌ

์ƒˆ ํ”„๋กœ์ ํŠธ - ์œ„์น˜๋ฅผ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํŒŒ์ผ์œ„์น˜๋กœ - ๊ธฐ๋ณธ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” 38์ด ๋“ค์–ด๊ฐ„ ๊ฒƒ์œผ๋กœ, main.py ์›ฐ์ปด ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ ์Šคํฌ๋ฆฝํŠธ๋Š” ํ•ด์ œ

=> ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ - ํŒŒ์ด์ฌ


๋ฌธ๋ฒ• (๋ณ€์ˆ˜, ์ž๋ฃŒํ˜•, ํ•จ์ˆ˜, ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ)

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฐฐ์šด ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‚˜, ์กฐ๊ธˆ ๋” ์ง๊ด€์ 
    ๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ :์œผ๋กœ ์ค„๋งž์ถค์œผ๋กœ ๋‚ด์šฉ๋ฌผ์„ ๊ตฌ๋ถ„ํ•จ

  • ๋ฐ˜๋ณต๋ฌธ
    - ๋ฆฌ์ŠคํŠธ ์ด์šฉ

fruits = ['์‚ฌ๊ณผ','๋ฐฐ','๋ฐฐ','๊ฐ','์ˆ˜๋ฐ•','๊ทค','๋”ธ๊ธฐ','์‚ฌ๊ณผ','๋ฐฐ','์ˆ˜๋ฐ•']

count = 0
for fruit in fruits:
	if fruit == '๋ฐฐ':
    	count += 1
        
print(count)

--๊ฒฐ๊ณผ--
3

  • ๋”•์…”๋„ˆ๋ฆฌ ์ด์šฉ
people = [{'name': 'bob', 'age': 20},
          {'name': 'carry', 'age': 38},
          {'name': 'john', 'age': 7},
          {'name': 'smith', 'age': 17},
          {'name': 'ben', 'age': 27}]

//๋ณดํ†ต ๋‹จ์ˆ˜, ๋ณต์ˆ˜์‹์œผ๋กœ ์‚ฌ์šฉ
for person in people:
    if person['age'] > 20:
        print(person['name'])

--๊ฒฐ๊ณผ--
carry
ben


07. ์›น์Šคํฌ๋ž˜ํ•‘(ํฌ๋กค๋ง) ๊ธฐ์ดˆ

  1. requests -> html ๊ฐ€์ ธ์˜ค๊ธฐ
  2. beatuifulsoup (๊ฐ€์ ธ์˜จ html ์—์„œ ์ œ๋ชฉ์„ ์‰ฝ๊ฒŒ ์ฐพ๊ฒŒ ํ•ด์คŒ) (์„ค์ •์—์„œ bs4 ํŒจํ‚ค์ง€ ์„ค์น˜)

ํฌ๋กค๋ง ๊ธฐ๋ณธ ์Šค๋‹ˆํŽซ

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'}
//ํ•˜๋‹จ url๋งŒ ํ•„์š”ํ•  ๋•Œ ๋ฐ”๊ฟ”์“ฐ๋ฉด ๋จ
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)

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

# ์ฝ”๋”ฉ ์‹œ์ž‘

์™ธ์šธ ํ•„์š” X ํ•„์š”ํ•  ๋•Œ ๋ณต์‚ฌํ•ด์„œ ํ•„์š”ํ•œ ๋ถ€๋ถ„ ๊ณ ์ณ์“ฐ๊ธฐ

์‹ค์Šต ์ฐธ๊ณ  ํŽ˜์ด์ง€: ๋„ค์ด๋ฒ„ ์˜ํ™” ํŽ˜์ด์ง€

beatiful soup ์“ฐ๋Š” ๋ฐฉ๋ฒ• (ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•X)

select_one() : ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉ

ex) ๋ฐฅ์ •

title = soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.title > div > a ')
print(title.text)

๊ฒฐ๊ณผ -> ๋ฐฅ์ •

select() : ์—ฌ๋Ÿฌ ๊ฐœ ์„ ํƒ

#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
#old_content > table > tbody > tr:nth-child(4) > td.title > div > a
์ด๋ ‡๊ฒŒ #old_content > table > tbody > tr๊นŒ์ง€๋Š” ๊ณตํ†ต๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์‹คํ–‰ํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ๋‹ˆ๊นŒ, for๋ฌธ์„ ๋Œ๋ ค์ค€๋‹ค.

movies = soup.select('#old_content > table > tbody > tr')

for movie in movies:
    a = movie.select_one('td.title > div > a')
    # none ์€ ๊ฐ€๋กœ์„ . ๋”ฐ๋ผ์„œ none์„ ์ œ์™ธํ•ด์ฃผ๊ณ  ๊ฐ€์•ผ ํ•จ
    if a is not None:
        print(a.text)

์‹คํ–‰๊ฒฐ๊ณผ

์ด๋Ÿฐ ์‹์œผ๋กœ ์˜ํ™” ์ด๋ฆ„์ด ์ญ‰ ๋‚˜์˜จ๋‹ค!


08. Quiz. ์›น์Šคํฌ๋ž˜ํ•‘ ํฌ๋กค๋ง ์—ฐ์Šต

๋ฐฐ์šด ๊ฒƒ์„ ํ† ๋Œ€๋กœ '์ˆœ์œ„'์™€ 'ํ‰์ '์„ ํฌ๋กค๋งํ•˜๊ธฐ

Q. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด์ด๋ฉด ์™„์„ฑ!

ํžŒํŠธ
movie.select_one() ์ด์šฉ!

A. ์™„์„ฑ์ฝ”๋“œ

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

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

# ์ฝ”๋”ฉ ์‹œ์ž‘
# ๊ณตํ†ต๋œ ๋ถ€๋ถ„
movies = soup.select('#old_content > table > tbody > tr')

for movie in movies:
    a = movie.select_one('td.title > div > a')
    # none ์€ ๊ฐ€๋กœ์„ . ๋”ฐ๋ผ์„œ none์„ ์ œ์™ธํ•ด์ฃผ๊ณ  ๊ฐ€์•ผ ํ•จ

    if a is not None:
        # ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋’ค์— ์žˆ๋Š” ๊ณตํ†ต๋ถ€๋ถ„ ์ ๊ธฐ
        title = a.text
        rank = movie.select_one('td:nth-child(1) > img')['alt']
        star = movie.select_one('td.point').text
        print(rank, title, star)
        

์‹คํ–‰๊ฒฐ๊ณผ


09. DB๊ฐœ๊ด„

DB๋Š” ์™œ ์“ฐ๋Š” ๊ฒƒ์ผ๊นŒ?
-> ๋‚˜์ค‘์— ์ž˜ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ
(๋ฐฉ ์ •๋ฆฌ ํ•˜๋Š” ์ด์œ , ๊ต๋ณด๋ฌธ๊ณ ์—์„œ ์ฑ…์ด ์ •๋ฆฌ๋˜๋Š” ์ด์œ ์™€ ์ผ๋งฅ์ƒํ†ต)

DB๋Š” index๋กœ ๋ฐ์ดํ„ฐ๋“ค์ด ์ •๋ ฌ๋˜์–ด ์žˆ์Œ

DB์˜ 2๊ฐ€์ง€ ์ข…๋ฅ˜
1) SQL - ์ •ํ•ด์ง„ ์นธ์ด ์žˆ์Œ
์žฅ - ์ •ํ˜•ํ™”๋˜์–ด ์žˆ์Œ, ์ฐพ์„ ๋•Œ ๋นจ๋ฆฌ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ
๋‹จ - ์œ ์—ฐํ•œ ๋Œ€์ฒ˜ ์–ด๋ ค์›€

2) NoSQL (Not Only SQL)- ์ •ํ•ด์ง„ ์นธ์ด ์—†์Œ
๊ทธ๋•Œ๊ทธ๋•Œ๋งˆ๋‹ค ์Œ“์ž„ -> ์œ ์—ฐํ•œ ๋Œ€์ฒ˜ ๊ฐ€๋Šฅ (์ดˆ๊ธฐ ์„œ๋น„์Šค, ์ดˆ๊ธฐ ์Šคํƒ€ํŠธ์—…์—์„œ ๋งŽ์ด ์‚ฌ์šฉ)
ex) mongoDB

DB๋ž€?
๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ์Œ“๊ณ  ์ž˜ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ํ”„๋กœ๊ทธ๋žจ
(์š”์ƒˆ๋Š” Cloudํ˜•ํƒœ๊ฐ€ ํŠธ๋ Œ๋“œ!)
์ˆ˜์—…์—์„œ๋Š” ์ตœ์‹  ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค mongoDB Atlas๋ฅผ ์‚ฌ์šฉ


10-11. mongoDB ์‹œ์ž‘, ์—ฐ๊ฒฐํ•˜๊ธฐ

์ ‘์†ํ•˜๊ธฐ ์œ„ํ•ด์„  ๋‘๊ฐ€์ง€ ํŒจํ‚ค์ง€ ํ•„์š”!!

pymango, dnspython

pymongo ๊ธฐ๋ณธ์ฝ”๋“œ

from pymongo import MongoClient
client = MongoClient('์—ฌ๊ธฐ์— URL ์ž…๋ ฅ')
db = client.dbsparta

url์€
์—ฌ๊ธฐ์— ์žˆ๋Š” url๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด ๋จ
password๋Š” ๋‚ด๊ฐ€ ์„ค์ •ํ•œ ๋น„๋ฒˆ์œผ๋กœ!

๋ฐ์ดํ„ฐ ๋“ค์–ด๊ฐ”๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฒ•

python์—์„œ

from pymongo import MongoClient
client = MongoClient('mongodb+srv://minjeong:rPfksakfdl1!@cluster0.mtbw8.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
db = client.dbsparta

doc = {
    'name':'bob',
    'age':27
}

db.users.insert_one(doc)

Browse Collectionํด๋ฆญ

์ด๋ ‡๊ฒŒ ๊ฒฐ๊ณผ๋ฌผ ํ™•์ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. users๋Š” python์ฝ”๋“œ์—์„œ db.users.insert_one(doc)์— ์žˆ๋Š” users์— ํ•ด๋‹นํ•œ๋‹ค.


12. pymongo๋กœ DB์กฐ์ž‘ํ•˜๊ธฐ

  • DB์—ฐ๊ฒฐํ•˜๊ธฐ & ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ
#'users'๋ผ๋Š” collection์— {'name':'bobby','age':21}๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.
db.users.insert_one({'name':'bobby','age':21})
db.users.insert_one({'name':'kay','age':27})
db.users.insert_one({'name':'john','age':30})

ํ†ต์ƒ์ ์œผ๋กœ๋Š” ์ด๋ ‡๊ฒŒ ์”€

doc = {'name':'bobby','age':21}
db.users.insert_one(doc)
  • ๋ชจ๋“  ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ณด๊ธฐ - pymongo(find)
#๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ
from pymongo import MongoClient
client = MongoClient('mongodb+srv://minjeong:rPfksakfdl1!@cluster0.mtbw8.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
db = client.dbsparta

all_users = list(db.users.find({}))

for user in all_users:
    print(user)

๊ฒฐ๊ณผ

#id ์ดํ•˜ ๋ถ€๋ถ„์€ ๋‚˜์˜ค์ง€ ์•Š๋Š”๋‹ค
all_users = list(db.users.find({},{'_id':False}))

for user in all_users:
    print(user)

# ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ฝ‘์•„๋ณด๊ธฐ
all_users = list(db.users.find({},{'_id':False}))

print(all_users[0])         # 0๋ฒˆ์งธ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด๊ธฐ
print(all_users[0]['name']) # 0๋ฒˆ์งธ ๊ฒฐ๊ณผ๊ฐ’์˜ 'name'์„ ๋ณด๊ธฐ

for user in all_users:      # ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ ๋ชจ๋“  ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด๊ธฐ
    print(user)
  • ํŠน์ • ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฝ‘์•„ ๋ณด๊ธฐ - pymongo(find_one)
user = db.users.find_one({'name':'bobby'})
print(user)

  • ์ˆ˜์ •ํ•˜๊ธฐ pymongo(update_one)
# ์˜คํƒ€๊ฐ€ ๋งŽ์œผ๋‹ˆ ์ด ์ค„์„ ๋ณต์‚ฌํ•ด์„œ ์”์‹œ๋‹ค!
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
#users์— ๋“ค์–ด๊ฐ€์„œ ์—…๋ฐ์ดํŠธ. ์กฐ๊ฑด : name์ด bobby๋ฅผ ๋ฐ”๊ฟ”. set : age๋ฅผ 19๋กœ ๋ณ€๊ฒฝํ•˜๋ผ
user = db.users.find_one({'name':'bobby'})
print(user)
  • ์‚ญ์ œํ•˜๊ธฐ (๊ฑฐ์˜ ์•ˆ ์”€) - pymongo(delete_one)
db.users.delete_one({'name':'bobby'})

user = db.users.find_one({'name':'bobby'})
print(user)

์š”์•ฝ! 4๊ฐ€์ง€ ๊ธฐ๋Šฅ ์œ„์ฃผ!!(์ €์žฅ, ์ฐพ๊ธฐ, ๋ฐ”๊พธ๊ธฐ, ์ง€์šฐ๊ธฐ)

# ์ €์žฅ - ์˜ˆ์‹œ
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)

# ํ•œ ๊ฐœ ์ฐพ๊ธฐ - ์˜ˆ์‹œ
user = db.users.find_one({'name':'bobby'})

# ์—ฌ๋Ÿฌ๊ฐœ ์ฐพ๊ธฐ - ์˜ˆ์‹œ ( _id ๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅ)
all_users = list(db.users.find({},{'_id':False}))

# ๋ฐ”๊พธ๊ธฐ - ์˜ˆ์‹œ
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

# ์ง€์šฐ๊ธฐ - ์˜ˆ์‹œ
db.users.delete_one({'name':'bobby'})

13.

import requests
from bs4 import BeautifulSoup

#๐ŸŒŸdb์—ฐ๊ฒฐํ•˜๊ธฐ!
from pymongo import MongoClient
client = MongoClient('mongodb+srv://minjeong:rPfksakfdl1!@cluster0.mtbw8.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
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.naver?sel=pnt&date=20210829',headers=headers)

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

# ์ฝ”๋”ฉ ์‹œ์ž‘
# ๊ณตํ†ต๋œ ๋ถ€๋ถ„
movies = soup.select('#old_content > table > tbody > tr')

for movie in movies:
    a = movie.select_one('td.title > div > a')
    # none ์€ ๊ฐ€๋กœ์„ . ๋”ฐ๋ผ์„œ none์„ ์ œ์™ธํ•ด์ฃผ๊ณ  ๊ฐ€์•ผ ํ•จ

    if a is not None:
        # ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋’ค์— ์žˆ๋Š” ๊ณตํ†ต๋ถ€๋ถ„ ์ ๊ธฐ
        title = a.text
        rank = movie.select_one('td:nth-child(1) > img')['alt']
        star = movie.select_one('td.point').text

	#๐ŸŒŸ10~13๊ฐ•์—์„œ ๋ฐฐ์› ๋˜ ๋ถ€๋ถ„์„ ํ† ๋Œ€๋กœ, doc์ƒ์„ฑ!!
        doc = {
            'title':title,
            'rank':rank,
            'star':star
        }
        db.movies.insert_one(doc)


14. Quiz

์ง„ํ–‰์‚ฌํ•ญ๊ณผ ์ƒ๊ด€์—†์ด ํ•œ๋ฒˆ ๋” ๋‹ค์‹œ ๋“ค์–ด์„œ ํ˜ผ์ž ์‹ค์Šตํ•ด๋ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค...

3๋ฒˆ


15. ๊ณผ์ œ

๐Ÿ“ƒ ์ง€๋‹ˆ๋ฎค์ง์˜ 1~50์œ„ ๊ณก์„ ์Šคํฌ๋ž˜ํ•‘ ํ•ด๋ณด์„ธ์š”.
์ง€๋‹ˆ๋ฎค์ง์‚ฌ์ดํŠธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์™„์„ฑ์ž„

ํžŒํŠธ
0) ์ถœ๋ ฅ ํ•  ๋•Œ๋Š” print(rank, title, artist) ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค!
1) ์•ž์—์„œ ๋‘ ๊ธ€์ž๋งŒ ๋Š๊ธฐ! text[0:2] ๋ฅผ ์จ๋ณด์„ธ์š”!
2) ์ˆœ์œ„์™€ ๊ณก์ œ๋ชฉ์ด ๊น”๋”ํ•˜๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„ ๊ฑฐ์˜ˆ์š”. ์˜†์— ์—ฌ๋ฐฑ์ด ์žˆ๋‹ค๋˜๊ฐ€, ๋‹ค๋ฅธ ๊ธ€์”จ๋„ ๋‚˜์˜จ๋‹ค๋˜๊ฐ€.. ํŒŒ์ด์ฌ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ strip()์„ ์ž˜ ์—ฐ๊ตฌํ•ด๋ณด์„ธ์š”!

HW ์ œ์ถœ ์ฝ”๋“œ

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

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

for music in musics:
    # ๋‹ค๋ฅธ ๋ถ€๋ถ„ ๋’ค์— ์žˆ๋Š” ๊ณตํ†ต๋ถ€๋ถ„ ์ ๊ธฐ
    rank = music.select_one('td.number').text[0:2].strip()
    title = music.select_one('td.info > a.title.ellipsis').text.strip()
    artist = music.select_one('td.info > a.artist.ellipsis').text

    print(rank, title, artist)

๋Š๋‚€ ์ 

์ข‹์€ ์ 

์•Œ์ฐฌ ์ˆ˜์—…์ž…๋‹ˆ๋‹ค! ์›น ๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ FE๋ถ€๋ถ„๊ณผ BE๋ถ€๋ถ„์„ ์กฐ๊ธˆ์ด๋ผ๋„ ๋‹ค๋ค„์„œ ๋งŒ์กฑ์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.

์•„์‰ฌ์šด ์ 

๋„“์€ ๋ฒ”์œ„๋ฅผ ์ข๊ฒŒ ๋ฐฐ์šฐ๋Š” ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค! ๋„“๊ฒŒ ๊ณจ๊ณ ๋ฃจ ๋ฐฐ์šฐ๋Š” ๊ฑด ์ข‹์ง€๋งŒ, ์ตํžˆ๋Š” ์‹œ๊ฐ„์ด ํ›จ์”ฌ ์งง๊ฒŒ ๋Š๊ปด์ง‘๋‹ˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๋” ์ฃผ๊ฑฐ๋‚˜ ์•„๋‹ˆ๋ฉด ๊ฐ•์˜๋ฅผ ๋” ์˜ค๋žœ ์‹œ๊ฐ„ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ณ„์  (5์ ๋งŒ์ ) : โญโญโญ

๋ฐฐ์šด ์ 

1. ํŒŒ์ด์ฌ
ํ•™๊ต ์ˆ˜์—…์—์„œ ๋ฐฐ์šด ํŒŒ์ด์ฌ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์‹ค์ „์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฑฐ๋ฅผ ๋ฐ”๋กœ ์ ‘ํ•œ ๋Š๋‚Œ์ด๋ผ ์ข‹์•˜๋‹ค. ํ˜ผ์ž์„œ ์›นํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์–ด๋–ค ํŒจํ‚ค์ง€๋ฅผ ์จ์•ผ ํ• ์ง€ ๋‹ค์†Œ ๋ง‰๋ง‰ํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ ๊ทธ๋ž˜๋„ '์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜๋Š” ๊ฑฐ๊ตฌ๋‚˜'๋ฅผ ๋งŽ์ด ๋Š๊ผˆ๋‹ค. ์ด๋Ÿฐ ๊ฐ ์กฐ์ฐจ ์—†์—ˆ๊ธฐ์— ์ƒˆ๋กœ์›Œ์„œ ์ข‹์•˜๋‹ค.
๋‚˜๋ฆ„ ํ•™๊ต์—์„œ ์ˆ˜์—… ๋“ค์—ˆ๋‹ค๊ณ  ์ž๋งŒํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๋‹ค ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์ด๋ผ์„œ ๋”ฐ๋ผ๊ฐ€๊ธด ํž˜๋“ค์—ˆ์ง€๋งŒ ์œ ์ตํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

ํŒŒ์ด์ฌ ๋ฆฌ๋งˆ์ธ๋“œ : ํฌ๋กค๋ง
๋‚˜์—๊ฒ ๋‹ค์†Œ ์–ด๋ ค์šด ๊ฐœ๋…์ด๋ผ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค!
select(), select_one()

2. DB
mongoDB ๊นŒ๋Š” ๋ถ€๋ถ„๋ถ€ํ„ฐ ์• ๋จน์—ˆ๋‹ค. ๊ฐ•์˜๊ฐ€ ์˜ˆ์ „๋ฒ„์ „์ด๋ผ์„œ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ์ง€ ๊ฐ์ด ์•ˆ ์™€์„œ ์‹œ๊ฐ„์ด ๊ฝค ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.
๊ทธ๋ž˜๋„ ์–ด์ฐŒ์ €์ฐŒ,,, ๊ฐ•์˜๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๊ธด ํ–ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒŒ ์˜จ๋ผ์ธ์ˆ˜์—…์˜ ๋‹จ์ ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๋ฐ”๋กœ๋ฐ”๋กœ ์งˆ๋ฌธ์„ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ. ์งˆ๋ฌธํ•ด๋„ ๋‹ต๋ณ€์„ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ๊ฒƒ.

๋‚˜์ค‘์—๋„ mongoDB๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ ค๋‚˜?

mongoDB ๋ฆฌ๋งˆ์ธ๋“œ : 4๊ฐ€์ง€ ์ฃผ์š” ๊ธฐ๋Šฅ (์ €์žฅ, ์ฐพ๊ธฐ, ์ˆ˜์ •ํ•˜๊ธฐ, ์‚ญ์ œํ•˜๊ธฐ)

# ์ €์žฅ - ์˜ˆ์‹œ
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)

# ํ•œ ๊ฐœ ์ฐพ๊ธฐ - ์˜ˆ์‹œ
user = db.users.find_one({'name':'bobby'})

# ์—ฌ๋Ÿฌ๊ฐœ ์ฐพ๊ธฐ - ์˜ˆ์‹œ ( _id ๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅ)
all_users = list(db.users.find({},{'_id':False}))

# ๋ฐ”๊พธ๊ธฐ - ์˜ˆ์‹œ
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

# ์ง€์šฐ๊ธฐ - ์˜ˆ์‹œ
db.users.delete_one({'name':'bobby'})

์‚ฌ์šฉํ•  ๋•Œ DB์˜ ์–ด๋Š ์œ„์น˜์— ์žˆ๋Š”์ง€ ๊ผญ ํ™•์ธํ•  ๊ฒƒ!

3. ๊ณผ์ œ
ํฌ๋กค๋งํ•˜๋Š” ๊ณผ์ œ์˜€๋‹ค. ์•ž์— DB๋ฐฐ์šด๋‹ค๊ณ  ๋‹ค ๊นŒ๋จน์—ˆ์–ด์„œ ๋ง‰๋ง‰ํ–ˆ๋‹ค. ์ „์— ํ–ˆ๋˜ hello.py๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.
๊ฐ•์˜ ๋“ฃ๊ณ  ๋”ฐ๋ผํ•  ๋•Œ๋Š” ๋ชจ๋ฅด๋Š”๋ฐ, ์ง์ ‘ ํ•ด๋ณด๋ฉด ๋” ๋งŽ์ด ๋ฐฐ์›Œ ๊ฐ€๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

profile
To be "irreplaceable"

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด