개발순서
API 만들기 ( POST )
리뷰 input 값 받아오기
입력된 url에서 타이틀/포스터/영화 정보들 스크래핑하기
1번,2번 정보 mongoDB에 저장하기
클라이언트 코드 만들기(리뷰 작성 / 리뷰 리스트 부분)
개발해보기
API 만들기 ( POST )
1) 리뷰 input 값 받아오기
@app.route('/review', methods=['POST'])
def write_review():
date_receive = request.form['date_give']
url_receive = request.form['url_give']
comment_receive = request.form['comment_give']
2) 입력된 url에서 타이틀/포스터/세부 정보들 스크래핑하기
# 스크래핑
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 스크래핑
og_image = soup.select_one('meta[property="og:image"]')
og_title = soup.select_one('meta[property="og:title"]')
url_poster = og_image['content']
url_title = og_title['content']
#세부정보 스크래핑
infos = soup.select('#content > div.article')
for info in infos:
genre = info.select_one('div.mv_info_area > div.mv_info > dl > dd:nth-child(2) > p > span:nth-child(1)').text.split(',')[0].strip()
image_fir = info.select_one('div.section_group.section_group_frst > div:nth-child(2) > div > ul > li:nth-child(2) > a.thumb_people > img')['src']
actor_fir = info.select_one('div.section_group.section_group_frst > div:nth-child(2) > div > ul > li:nth-child(2) > a.thumb_people > img')['alt']
image_sec = info.select_one('div.section_group.section_group_frst > div:nth-child(2) > div > ul > li:nth-child(3) > a.thumb_people > img')['src']
actor_sec = info.select_one('div.section_group.section_group_frst > div:nth-child(2) > div > ul > li:nth-child(3) > a.thumb_people > img')['alt']
image_trd = info.select_one('div.section_group.section_group_frst > div:nth-child(2) > div > ul > li:nth-child(4) > a.thumb_people > img')['src']
actor_trd = info.select_one('div.section_group.section_group_frst > div:nth-child(2) > div > ul > li:nth-child(4) > a.thumb_people > img')['alt']
3) 1번,2번 정보 mongoDB에 저장하기
doc = {
'date': date_receive,
'url': url_receive,
'poster' : url_poster,
'title': url_title,
'genre': genre,
'image_fir': image_fir,
'actor_fir': actor_fir,
'image_sec': image_sec,
'actor_sec': actor_sec,
'image_trd': image_trd,
'actor_trd': actor_trd,
'comment': comment_receive
}
db.reviews.insert_one(doc)
return jsonify({'result': 'success', 'msg': '리뷰가 저장되었습니다.'})
4) 클라이언트 코드 만들기(리뷰 작성 / 리뷰 리스트 부분)
$.ajax({
type: "POST",
url: "/review",
data: {date_give: date, url_give: url, comment_give: comment},
success: function (response) {
if (response["result"] == "success") {
alert(response["msg"]);
window.location.reload();
}
}
})
여러 시행착오를 거쳐서 완성된 결과물
이제 GET방식을 사용해 저장된 데이터를 화면에 띄워보자!