커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
bson.errors.InvalidDocument 오류
undefined주차
북마크
박*혜
댓글
4
추천
0
조회수
8
조회수
8
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


코드 붙여넣어서 다시 올립니다!



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

import requests
from bs4 import BeautifulSoup

from pymongo import MongoClient
import certifi

ca = certifi.where()

client = MongoClient('mongodb+srv://test:sparta@cluster0.xqvw0op.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')

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

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

    db.movies.insert_one(doc)

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

@app.route("/mars", methods=["GET"])
def mars_get():
    orders_list = list(db.orders.find({},{'_id':False}))
    return jsonify({'orders':orders_list})

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





127.0.0.1 - - [04/Feb/2023 19:53:25] "POST /movie HTTP/1.1" 500 -

Traceback (most recent call last):

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2548, in __call__

  return self.wsgi_app(environ, start_response)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2528, in wsgi_app

  response = self.handle_exception(e)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2525, in wsgi_app

  response = self.full_dispatch_request()

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1822, in full_dispatch_request

  rv = self.handle_user_exception(e)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1820, in full_dispatch_request

  rv = self.dispatch_request()

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1796, in dispatch_request

  return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)

 File "/Users/jihye/Desktop/preliminary-study/스파르타피디아/app.py", line 43, in movie_post

  db.movies.insert_one(doc)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/collection.py", line 628, in insert_one

  self._insert_one(

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/collection.py", line 569, in _insert_one

  self.__database.client._retryable_write(acknowledged, _insert_command, session)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1476, in _retryable_write

  return self._retry_with_session(retryable, func, s, None)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1349, in _retry_with_session

  return self._retry_internal(retryable, func, session, bulk)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/_csot.py", line 105, in csot_wrapper

  return func(self, *args, **kwargs)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1390, in _retry_internal

  return func(session, sock_info, retryable)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/collection.py", line 557, in _insert_command

  result = sock_info.command(

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/pool.py", line 795, in command

  self._raise_connection_failure(error)

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/pool.py", line 767, in command

  return command(

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/network.py", line 127, in command

  request_id, msg, size, max_doc_size = message._op_msg(

 File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymongo/message.py", line 692, in _op_msg

  return _op_msg_uncompressed(flags, command, identifier, docs, opts)

bson.errors.InvalidDocument: cannot encode object: <meta content="https://movie-phinf.pstatic.net/20230120_114/16742051296728IGUd_JPEG/movie_image.jpg?type=m665_443_2" property="og:image"/>, of type: <class 'bs4.element.Tag'>




취소
 공유
취소
댓글 0
댓글 알림
나의얼굴