VS Code Python 패키지 설치하기
- pip install flask (서버를 구동시켜주는 편한 코드 모음)
flask 시작 코드
render_template (HTML을 가져온다.)
from flask import Flask ,render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
GET 요청 API 코드
@app.route('/test', methods=['GET'])
def test_get():
title_receive = request.args.get('title_give')
print(title_receive)
return jsonify({'result':'success', 'msg': '이 요청은 GET!'})
jsonify
- 사용자가 json data를 내보내도록 제공.
GET 요청 확인Fetch 코드
function hey() {
fetch("/test").then(res => res.json()).then(data => {
console.log(data)
})
}
POST 요청 API 코드
@app.route('/test', methods=['POST'])
def test_post():
title_receive = request.form['title_give']
print(title_receive)
return jsonify({'result':'success', 'msg': '이 요청은 POST!'})
POST 요청 확인 Fetch 코드
let formData = new FormData();
formData.append("title_give", "블랙팬서");
fetch("/test", { method: "POST", body: formData }).then(res => res.json()).then(data => {
console.log(data)
})
mars 프로젝트
Fetch POST
function save_order() {
let name = $('#name').val()
let address = $('#address').val()
let size = $('#size').val()
let formData = new FormData();
formData.append('name_give', name)
formData.append('address_give',address)
formData.append('size_give' , size)
fetch('/mars', { method: "POST", body: formData }).then((res) => res.json()).then((data) => {
alert(data["msg"])
window.location.reload()
});
}
Fetch GET
$(document).ready(function () {
show_order();
});
function show_order() {
fetch('/mars').then((res) => res.json()).then((data) => {
$('#order-box').empty()
let rows = data['order_list']
rows.forEach((a) =>{
let name = a['name']
let address = a['address']
let size = a['size']
let temp_html = ` <tr>
<th scope="col">${name}</th>
<th scope="col">${address}</th>
<th scope="col">${size}</th>
</tr>`
$('#order-box').append(temp_html)
})
})
}
bs4 og
import requests
from bs4 import BeautifulSoup
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('https://movie.naver.com/movie/bi/mi/basic.naver?code=191597',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']
print(ogtitle, ogimage, ogdesc)
'HTML, CSS, javascript' 카테고리의 다른 글
웹 프로그래밍 기초. 5 미니프로젝트, 배포하기 (0) | 2023.03.20 |
---|---|
웹 프로그래밍 기초. 3 Python, 크롤링, mongoDB (0) | 2023.03.18 |
웹 프로그래밍 기초. 2 javascript, jQuery, Fetch (0) | 2023.03.17 |
웹 프로그래밍 기초. 1 HTML CSS (0) | 2023.03.17 |