본문 바로가기

미니 프로젝트

[6] 스파르타피디아(전 무비스타)

스파르타 피디아

내 생애 최고의 영화들

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.dbsparta

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

    # 여기에 코딩을 해서 meta tag를 먼저 가져와보겠습니다.
    title = soup.select_one('meta[property="og:title"]')['content']
    img = soup.select_one('meta[property="og:image"]')['content']
    description = soup.select_one('meta[property="og:description"]')['content']

    print(title, img, description)

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

    db.movie.insert_one(doc)

    return jsonify({'msg':'저장 완료!'})

@app.route("/movie", methods=["GET"])
def movie_get():
    movie_list = list(db.movie.find({}, {'_id': False}))
    return jsonify({'movies':movie_list})

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

'미니 프로젝트' 카테고리의 다른 글

[4] 한 가지 쇼핑몰 - 웹개발 종합반 프로젝트  (0) 2022.06.30
[7] 나의 버킷리스트  (0) 2022.06.30
[5] 화성땅 공동구매  (0) 2022.06.30
[3] 무비스타  (0) 2022.06.27
[2] 책 리뷰  (0) 2022.06.27