Weekly I Learned (4์ฃผ์ฐจ)

Parkbossยท2022๋…„ 10์›” 21์ผ
0

๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
7/120
post-thumbnail

4์ฃผ ์ฐจ ๐Ÿ“ข

Flask ๋ž€?

๐Ÿ‘‰ Flask ํ”„๋ ˆ์ž„์›Œํฌ: ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™์‹œ์ผœ์ฃผ๋Š” ํŽธํ•œ ์ฝ”๋“œ ๋ชจ์Œ. ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™ํ•˜๋ ค๋ฉด ํ•„์š”ํ•œ ๋ณต์žกํ•œ ์ผ๋“ค์„ ์‰ฝ๊ฒŒ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ๐Ÿ’ป

  • Flask ๊ธฐ์ดˆ URL ๋‚˜๋ˆ ๋ณด๊ธฐ
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
   return 'This is Home!'

@app.route('/mypage')
def mypage():  
   return 'This is My Page!'

if __name__ == '__main__':  
   app.run('0.0.0.0',port=5000,debug=True)

Flask ๊ธฐ๋ณธ ํด๋” ๊ตฌ์กฐ

Flask ์„œ๋ฒ„๋ฅผ ๋งŒ๋“ค ๋•Œ, ํ•ญ์ƒ,

ํ”„๋กœ์ ํŠธ ํด๋” ์•ˆ์—,
 ใ„ดstatic ํด๋” (์ด๋ฏธ์ง€, cssํŒŒ์ผ์„ ๋„ฃ์–ด๋‘ก๋‹ˆ๋‹ค)
 ใ„ดtemplates ํด๋” (htmlํŒŒ์ผ์„ ๋„ฃ์–ด๋‘ก๋‹ˆ๋‹ค)
 ใ„ดapp.py ํŒŒ์ผ

์ด๋ ‡๊ฒŒ ์„ธ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด๋‘๊ณ  ์‹œ์ž‘ํ•˜์„ธ์š”. ์ด์ œ ๊ฐ ํด๋”์˜ ์—ญํ• ์„ ์•Œ์•„๋ด…์‹œ๋‹ค!

(๊ผญ ์ฐธ๊ณ !! venv๋Š” ์‹ค์ œ๋กœ๋Š” ๋ณด์ด์ง€๋งŒ, ์•ˆ๋ณด์ธ๋‹ค~๋ผ๊ณ  ์ƒ๊ฐํ•˜์„ธ์š”! ๊ธฐ์–ตํ•˜์‹œ์ฃ ?)

์˜ˆ์ œ ๐Ÿ’ป

  • flask ๋‚ด์žฅํ•จ์ˆ˜ render_template๋ฅผ ์ด์šฉํ•˜์—ฌ html ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ”๋กœ ์ด๊ฒŒ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์œ„๋ ฅ!
from flask import Flask, render_template
app = Flask(__name__)

## URL ๋ณ„๋กœ ํ•จ์ˆ˜๋ช…์ด ๊ฐ™๊ฑฐ๋‚˜,
## route('/') ๋“ฑ์˜ ์ฃผ์†Œ๊ฐ€ ๊ฐ™์œผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

@app.route('/')
def home():
   return render_template('index.html')

if __name__ == '__main__':
   app.run('0.0.0.0', port=5000, debug=True)

GET, POST ์š”์ฒญ ํƒ€์ž…

๐Ÿ‘‰ GET โ†’ ํ†ต์ƒ์ ์œผ๋กœ! ๋ฐ์ดํ„ฐ ์กฐํšŒ(Read)๋ฅผ ์š”์ฒญํ•  ๋•Œ
์˜ˆ) ์˜ํ™” ๋ชฉ๋ก ์กฐํšŒ
โ†’ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ : URL ๋’ค์— ๋ฌผ์Œํ‘œ๋ฅผ ๋ถ™์—ฌ key=value๋กœ ์ „๋‹ฌ
โ†’ ์˜ˆ: google.com?q=๋ถ๊ทน๊ณฐ

๐Ÿ‘‰ POST โ†’ ํ†ต์ƒ์ ์œผ๋กœ! ๋ฐ์ดํ„ฐ ์ƒ์„ฑ(Create), ๋ณ€๊ฒฝ(Update), ์‚ญ์ œ(Delete) ์š”์ฒญ ํ•  ๋•Œ
์˜ˆ) ํšŒ์›๊ฐ€์ž…, ํšŒ์›ํƒˆํ‡ด, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ˆ˜์ •
โ†’ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ : ๋ฐ”๋กœ ๋ณด์ด์ง€ ์•Š๋Š” HTML body์— key:value ํ˜•ํƒœ๋กœ ์ „๋‹ฌ


1. ์—ฐ์Šต ์˜ˆ์ œ ๋ผˆ๋Œ€ ์ค€๋น„ํ•˜๊ธฐ

  • 1) app.py ์ค€๋น„ํ•˜๊ธฐ
  • 2) index.html ์ค€๋น„ํ•˜๊ธฐ
  • 3) mongoDB Atlas ์ฐฝ ๋„์›Œ๋‘๊ธฐ

2. POST ์—ฐ์Šต (์ฃผ๋ฌธ ์ €์žฅ)

1. ์š”์ฒญ ์ •๋ณด :  URL= /mars, ์š”์ฒญ ๋ฐฉ์‹ = POST
2. ํด๋ผ(ajax) โ†’ ์„œ๋ฒ„(flask) : name, address, size
3. ์„œ๋ฒ„(flask) โ†’ ํด๋ผ(ajax) : ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„ (์ฃผ๋ฌธ ์™„๋ฃŒ!)
  • ํด๋ผ์ด์–ธํŠธ index.html์™€ ์„œ๋ฒ„ app.py ์—ฐ๊ฒฐ ํ™•์ธํ•˜๊ธฐ
  • name, address, size ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ, ์ €์žฅํ•˜๋ฉด ๋˜๊ฒ ์ฃ ?
  • ์šฐ๋ฆฌ๊ฐ€ ์ผ์ „์— ๋งŒ๋“ค์–ด๋‘” [dbtest.py](http://dbtest.py) ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
@app.route("/mars", methods=["POST"])
def mars_post():
    name_receive = request.form['name_give']
    address_receive = request.form['address_give']
    size_receive = request.form['size_give']

    doc = {
        'name': name_receive,
        'address': address_receive,
        'size': size_receive
    }

    db.orders.insert_one(doc)

    return jsonify({'msg': '์ฃผ๋ฌธ ์™„๋ฃŒ!'})
  • name, address, size ์ •๋ณด๋ฅผ ๋ณด๋‚ด์ค€๋‹ค.
function save_order() {
    let name = $('#name').val()
    let address = $('#address').val()
    let size = $('#size').val()

    $.ajax({
        type: 'POST',
        url: '/mars',
        data: { name_give:name, address_give:address, size_give:size },
        success: function (response) {
            alert(response['msg'])
            window.location.reload()
        }
    });
}

3. GET ์—ฐ์Šต (์ฃผ๋ฌธ ๋ณด์—ฌ์ฃผ๊ธฐ)

  • ๋ฐ›์„ ๊ฒƒ ์—†์ด orders ์— ์ฃผ๋ฌธ์ •๋ณด๋ฅผ ๋‹ด์•„์„œ ๋‚ด๋ ค์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค
@app.route("/mars", methods=["GET"])
def mars_get():
    orders_list = list(db.orders.find({},{'_id':False}))
    return jsonify({'orders':orders_list})
  • ์‘๋‹ต์„ ์ž˜ ๋ฐ›์•„์„œ for ๋ฌธ์œผ๋กœ! ๋ถ™์—ฌ์ฃผ๋ฉด ๋
function show_order() {
    $('#order-box').empty()
    $.ajax({
        type: 'GET',
        url: '/mars',
        data: {},
        success: function (response) {
            let rows = response['orders']
            for (let i = 0; i < rows.length; i++) {
                let name = rows[i]['name']
                let address = rows[i]['address']
                let size = rows[i]['size']

                let temp_html = `<tr>
                                    <td>${name}</td>
                                    <td>${address}</td>
                                    <td>${size}</td>
                                  </tr>`
                $('#order-box').append(temp_html)
            }

        }
    });
}

meta ํƒœ๊ทธ ์Šคํฌ๋ž˜ํ•‘

๐Ÿ‘‰์ œ๋ชฉ, ์ธ๋„ค์ผ ์ด๋ฏธ์ง€, ๋‚ด์šฉ์ด๋‹ค.
์ด ๋ถ€๋ถ„์€, 'meta'ํƒœ๊ทธ๋ฅผ ํฌ๋กค๋ง ํ•จ์œผ๋กœ์จ ๊ณตํ†ต์ ์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฉ”ํƒ€ ํƒœ๊ทธ๋Š”, ๋ถ€๋ถ„์— ๋“ค์–ด๊ฐ€๋Š”, ๋ˆˆ์œผ๋กœ ๋ณด์ด๋Š” ๊ฒƒ(body) ์™ธ์— ์‚ฌ์ดํŠธ์˜ ์†์„ฑ์„ ์„ค๋ช…ํ•ด์ฃผ๋Š” ํƒœ๊ทธ๋“ค์ž…๋‹ˆ๋‹ค.

  • ์˜ˆ) ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰ ์‹œ ํ‘œ์‹œ ๋  ์„ค๋ช…๋ฌธ, ์‚ฌ์ดํŠธ ์ œ๋ชฉ, ์นดํ†ก ๊ณต์œ  ์‹œ ํ‘œ์‹œ ๋  ์ด๋ฏธ์ง€ ๋“ฑ

  • head์— meta[property="og:image"]์„ ๊ฐ€์ ธ์˜จ๋‹ค.

import requests
from bs4 import BeautifulSoup

url = 'https://movie.naver.com/movie/bi/mi/basic.naver?code=191597'

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(url,headers=headers)

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

    og_image = soup.select_one('meta[property="og:image"]')
    og_title = soup.select_one('meta[property="og:title"]')
    og_description = soup.select_one('meta[property="og:description"]')

    image = og_image['content']
    title = og_title['content']
    description = og_description['content']    
  

1. ์˜ํ™” ์—ฐ์Šต 2 ์˜ˆ์ œ ๋ผˆ๋Œ€ ์ค€๋น„ํ•˜๊ธฐ

  • 1) app.py ์ค€๋น„ํ•˜๊ธฐ
  • 2) index.html ์ค€๋น„ํ•˜๊ธฐ
  • 3) mongoDB Atlas ์ฐฝ ๋„์›Œ๋‘๊ธฐ

2. POST ์—ฐ์Šต (ํฌ์ŠคํŒ…ํ•˜๊ธฐ)

  • ์„œ๋ฒ„ app.py ์—ฐ๊ฒฐ
  • url, star, comment ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ ์ €์žฅํ•˜๊ธฐ
  @app.route("/movie", methods=["POST"])
def movie_post():
    url_receive = request.form['url_give']
    star_receive = request.form['star_give']
    comment_receive = request.form['comment_give']

    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(url_receive, headers=headers)

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

    og_image = soup.select_one('meta[property="og:image"]')
    og_title = soup.select_one('meta[property="og:title"]')
    og_description = soup.select_one('meta[property="og:description"]')

    image = og_image['content']
    title = og_title['content']
    description = og_description['content']

    doc = {
        'image':image,
        'title':title,
        'desc':description,
        'star':star_receive,
        'comment':comment_receive
    }

    db.movies.insert_one(doc)

    return jsonify({'msg':'POST ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})
  • ํด๋ผ์ด์–ธํŠธ index.html ์—ฐ๊ฒฐ
  function posting() {
    let url = $('#url').val()
    let star = $('#star').val()
    let comment = $('#comment').val()

    $.ajax({
        type: 'POST',
        url: '/movie',
        data: {url_give: url, star_give: star, comment_give: comment},
        success: function (response) {
            alert(response['msg'])
            window.location.reload()
        }
    });
}

3. GET ์—ฐ์Šต (๋ณด์—ฌ์ฃผ๊ธฐ)

  • ์„œ๋ฒ„ app.py ์—ฐ๊ฒฐ
@app.route("/movie", methods=["GET"])
def movie_get():
    movie_list = list(db.movies.find({},{'_id':False}))
    return jsonify({'movies':movie_list})
  • ํด๋ผ์ด์–ธํŠธ index.html ์—ฐ๊ฒฐ
function listing() {
    $('#cards-box').empty()
    $.ajax({
        type: 'GET',
        url: '/movie',
        data: {},
        success: function (response) {
            let rows = response['movies']
            for(let i = 0; i < rows.length; i++) {
                let image = rows[i]['image']
                let title = rows[i]['title']
                let desc = rows[i]['desc']
                let star = rows[i]['star']
                let comment = rows[i]['comment']

                let star_image = 'โญ'.repeat(star)

                let temp_html = `<div class="col">
                                    <div class="card h-100">
                                        <img src="${image}"
                                             class="card-img-top">
                                        <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)
            }
        }
    })
}

๐Ÿ‘‰ ๋ง›๋ณด๊ธฐ ์ˆ™์ œ

profile
ur gonna figure it out. just like always have.

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