개발/문제해결

서버로부터 get이 안되는 경우 문제 발생

Mo_bi!e 2022. 10. 26. 14:57

 

 

소스코드

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

import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://mo:jae@cluster0.joa3ijk.mongodb.net/?retryWrites=true&w=majority', tlsCAFile=ca)
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를 먼저 가져와보겠습니다.
    # print(soup)

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

    doc = {
        'title' : title,
        'image' : image,
        'desc' : desc,
        'star' : star_receive,
        'comment' : comment_receive
    }
    db.movies.insert_one(doc)

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

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

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

<문제점>

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

 

본 소스코드에서 get()함수에서 반환을 문자열로 리턴을 하여서 get()한 결과물이 표시되지 못했다.

 

<해결방법>

'movies':movie_list

로 바꾸어 주니 해결이 되었다.