본문 바로가기
TIL/내배캠 사전캠프

23.05.08

by J1-H00N 2023. 5. 8.

새로운 조원들과 함께 미니 프로젝트를 만들게 되었다.

팀 이름은 비전파이브

일단 영화 추천사이트를 만들기로 했고, 그 구성은 아래와 같다.

 

나는 백엔드(python)를 맡게 되었다.

 

기본 코드 작성

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():
    url_receive = request.form['url_give']
    comment_receive = request.form['comment_give']
    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)

아틀라스 mongodb 연결

from pymongo import MongoClient
client = MongoClient('mongodb+srv://sparta:test@cluster0.szcxpef.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

 

post부분 코드 만들기

일단 등록 페이지에서 가져온 값들 저장

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

@app.route("/movie", methods=["POST"])
def movie_post():
    url_receive = request.form['url_give']
    # 입력한 url 저장
    comment_receive = request.form['comment_give']
    # 입력한 comment 저장
    star_receive = request.form['star_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')

    ogtitle = soup.select_one('meta[property="og:title"]')['content']
    ogimage = soup.select_one('meta[property="og:image"]')['content']
    ogdesc = soup.select_one('meta[property="og:description"]')['content']
    # meta tag에서 제목, 이미지, 간단 설명 스크랩하는 코드

    doc = {
        'title' : ogtitle,
        'desc' : ogdesc,
        'image' : ogimage,
        'comment' : comment_receive,
        'star' : star_receive
    }
    db.main.insert_one(doc)
    # main이라는 콜렉션 생성, 데이터들 가져와서 저장하기

    return jsonify({'msg':'등록 완료!'}) # 위 코드가 정상 작동 시 등록 완료라는 메시지 반환

'TIL > 내배캠 사전캠프' 카테고리의 다른 글

23.05.11  (0) 2023.05.11
23.05.11  (0) 2023.05.11
23.05.10  (0) 2023.05.10
23.05.09  (0) 2023.05.09