๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/TIL(Today I Learned)

TIL_220505_ํŒ€ ํ”„๋กœ์ ํŠธ

  • -

 


 

 

๋ฌด์—‡์„ ๋Š๊ผˆ๋Š”๊ฐ€.

 

 

 


 

 

[์˜ค๋Š˜ ํ•˜๋ฃจ ํ•„๊ธฐ์‹ ๋ฐฐ์šด ๋‚ด์šฉ ๊ธฐ๋กํ•˜๊ธฐ]

 

 

ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ ์ „ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์‚ฌ์ „ ์„ค์น˜

  • flask, pymongo, dnspython

ํ”„๋กœ์ ํŠธ ์„œ๋ฒ„ ๊ตฌ์ถ•

from flask import Flask, render_template, request, jsonify
app = Flask(__name__)

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

@app.route("/movie", methods=["POST"])
def movie_post():
    sample_receive = request.form['sample_give']
    print(sample_receive)

    return jsonify({'msg':'POST ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})

@app.route("/movie", methods=["GET"])
def movie_get():

    return jsonify({'msg':'GET ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})

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

 

๋ฐ์ดํ„ฐ DB ์ €์žฅ ํผ

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

import requests
from bs4 import BeautifulSoup

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

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

@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')

    image = soup.select_one('meta[property="og:image"]')['content']
    title = soup.select_one('meta[property="og:title"]')['content']
    desc = soup.select_one('meta[property="og:description"]')['content']


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

    db.movies.insert_one(doc)

    return jsonify({'msg': '์ €์žฅ ์™„๋ฃŒ!'})


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

 

ํฌ์ŠคํŒ… API

  1. ํด๋ผ์ด์–ธํŠธ์—์„œ ์ค„ ๋ฐ์ดํ„ฐ: ์ œ๋ชฉ, ์ฝ”๋ฉ˜ํŠธ
  2. ์„œ๋ฒ„์—์„œ ํ•  ์ผ: ์ œ๋ชฉ, ์ฝ”๋ฉ˜ํŠธ๋ฅผ DB์— ์ €์žฅ
  3. ์„œ๋ฒ„์—์„œ ์ค„ ๊ฒƒ: ์ž˜ ์ €์žฅ๋๋‹ค๋Š” ๋ฉ”์‹œ์ง€

 

๋ฆฌ์ŠคํŒ… API

  1. ํด๋ผ์ด์–ธํŠธ์—์„œ ์ค„ ๋ฐ์ดํ„ฐ: ์—†์Œ
  2. ์„œ๋ฒ„์—์„œ ํ•  ์ผ: ์ œ๋ชฉ, ์ฝ”๋ฉ˜ํŠธ๋ฅผ DB์—์„œ ๋ชจ๋‘ ์ถ”์ถœ
  3. ์„œ๋ฒ„์—์„œ ์ค„ ๊ฒƒ: ์ œ๋ชฉ, ์ฝ”๋ฉ˜ํŠธ ๊พธ๋Ÿฌ๋ฏธ(๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ)

'๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ > TIL(Today I Learned)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL_220507_DB ์˜ค๋ฅ˜  (0) 2023.01.01
TIL_220506_ํŒ€ ํ”„๋กœ์ ํŠธ  (0) 2023.01.01
TIL_220504  (0) 2023.01.01
TIL_220503_ํŒ€ ํ”„๋กœ์ ํŠธ S.A  (0) 2023.01.01
TIL_220502_KPT  (0) 2023.01.01
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.