: ์๋ฒ๋ฅผ ๊ตฌ๋์์ผ์ฃผ๋ ํธํ ์ฝ๋ ๋ชจ์. ์๋ฒ๋ฅผ ๊ตฌ๋ํ๋ ค๋ฉด ํ์ํ ๋ณต์กํ ์ผ๋ค์ ์ฝ๊ฒ ๊ฐ์ ธ๋ค ์
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'This is Home!'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
flask ๊ธฐ๋ณธ๊ตฌ์กฐ
static : css๋ ์ด๋ฏธ์งํ์ผ์ ๋ด์๋ ๋ ์ฌ์ฉ
templates : html ํ์ผ์ ๋ด์๋ ๋ ์ฌ์ฉ
from flask import Flask, render_template #render_template ์ถ๊ฐ
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html') #templates ํด๋์ index.html์ ์ฐ๊ฒฐ
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
import requests
from bs4 import BeautifulSoup
url = 'https://movie.naver.com/movie/bi/mi/basic.nhn?code=171539' #๊ฐ์ ธ์ฌ ์ํ 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,headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# title = soup.select_one('head > meta:nth-child(9)')
# print(title)
# ๋ฉํ ํ๊ทธ๋ก ํฌ๋กค๋ง ํด์ค๋ ๋ฐฉ๋ฒ
title = soup.select_one('meta[property="og:title"]')['content']
image = soup.select_one('meta[property="og:url"]')['content']
desc = soup.select_one('meta[property="og:description"]')['content']
print(title, image, desc)
# title = soup.select_one('head > meta:nth-child(9)')
# print(title)
title = soup.select_one('meta[property="og:title"]')['content']
image = soup.select_one('meta[property="og:url"]')['content']
desc = soup.select_one('meta[property="og:description"]')['content']
@app.route('/memo', methods=['POST'])
def saving():
url_receive = request.form['url_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')
# ๋ฉํ ํ๊ทธ๋ก ํฌ๋กค๋ง ํด์ค๋ ๋ฐฉ๋ฒ
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,
'url': url_receive,
'comment': comment_receive
}
db.articles.insert_one(doc)
return jsonify({'msg': '์ ์ฅ์ด ์๋ฃ ๋์์ต๋๋ค!'})
function postArticle() {
let url = $('#post-url').val()
let comment = $('#post-comment').val()
$.ajax({
type: "POST",
url: "/memo",
data: {url_give : url, comment_give : comment},
success: function ( response) { // ์ฑ๊ณตํ๋ฉด
alert(response["msg"]);
window.location.reload() //์๋ก๊ณ ์นจ
}
})
}
`
`
`
<div id="post-box" class="form-post" style="display:none">
<div>
<div class="form-group">
<label for="post-url">์ํฐํด URL</label> <!-- url์ด ์
๋ ฅ๋๋๋ถ๋ถ -->
<input id="post-url" class="form-control" placeholder="">
</div>
<div class="form-group">
<label for="post-comment">๊ฐ๋จ ์ฝ๋ฉํธ</label>
<textarea id="post-comment" class="form-control" rows="2"></textarea>
</div>
<button type="button" class="btn btn-primary" onclick="postArticle()">๊ธฐ์ฌ์ ์ฅ</button>
</div>
</div>
@app.route('/memo', methods=['GET'])
def listing():
articles = list(db.articles.find({}, {'_id': False}))
return jsonify({'all_articles': articles})
function showArticles() {
$.ajax({
type: "GET",
url: "/memo",
data: {},
success: function (response) {
let articles = response[`all_articles`]
for(let i = 0; i < articles.length; i++) {
let title = articles[i]['title']
let image = articles[i]['image']
let url = articles[i]['url']
let desc = articles[i]['desc']
let comment = articles[i]['comment']
let temp_html = ` <div class="card">
<img class="card-img-top"
src="${image}"
alt="Card image cap">
<div class="card-body">
<a target="_blank" href="${url}" class="card-title">${title}</a>
<p class="card-text">${desc}</p>
<p class="card-text comment">${comment}</p>
</div>`
$('#cards-box').append(temp_html)
}
}
})
}