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)
# render_template는 falsk의 내장함수로 template폴더를 랜더링 하는 함수이다.
# 서버 -> 로컬에 index.html 파일을 싸줘!
GET요청을 해보자
get요청은 데이터를 조회할 때 사용한다!
function hey() {
$.ajax({
type: "GET",
url: "/test?title_give=봄날은간다",
//test(창구)
//title_give라는 이름으로(주민등록번호)
//봄날은 간다 라는 데이터를 보내준다.(숫자)
data: {},
success: function(response){
console.log(response)
}
})
}
// , 유의하기..
@app.route('/test', meghods=['GET'])
def test_get():
title_receive = request.args.get('title_give')
print(title_receive)
return jsonify({'result':'success', 'msg':'이 요청은 GET'})
test라는 창구에서 test_give이라는 이름으로 데이터를 받아와서
title_receive에 담고 그리고 그걸 print해줘
반환값으로 저것을
# jsonfify : json데이터를 처리하기위해. 리턴할 때 써준다.
# 데이터를 처리하기위해 라는것은 로컬로 쏴주는걸 잘되게해주기위함인가?
# flask의 내장함수 : request, jsonify추가!
app.py에 GET요청을 받을 준비를 한다.
창구명, 어떤 이름으로 받을지 등등의 내용을 적는다.
index.html>script(또는 js파일)에서 GET요청을 받을 준비를한다.
창구명을 적고 어떤 데이터를 받을지(url) 적고, 성공하면 console에 찍어주는 코드를 적는다.
app.py를 실행시킨다!!
function hey() {
$.ajax({
type: "POST",
url : "/test",
data: {title_give: '봄날은 간다'},
success: function(response){
console.log(response) //return값이나온다. 만약 [msg]를 받고싶으면 response['msg']조회!
}
})
}
///test의 title_give:봄날을 간다 를 가져온다.
@app.route('/test', methods=['POST'])
def test_post():
title_receive = request.form['title_give'] #봄날을 간다를 가지고왔어!
print(title_receive)
return jsonify({'result':'success', 'msg': '이요청은POST'}) #이제 너한테 이값을 보낼거야
#/test라는 창구를 우리가 만들었고 POST요청인 경우 아래def을 따라라
#title_give를 받아와서 print해줘
#return은 동일하게 json형태로 해줘.
POST 요청을 통해서 서버가 해야되는 일은 무엇인가?
이름, 주소, 평수의 데이터를 주문하기 버튼을 클릭하는 순간 DB에 쏴줘야 한다.
from pymongo import MongoClient
client = MongoClient('DB주소')
db = client.dbsparta
@app.route("/mars", methods=["POST"])
def web_mars_post():
name_receive = request.form['name_give']
address_receive = request.form['address_give']
size_receive = request.form['size_give']
doc = { 'name': name_receive , 'address' : address_receive, 'size': size_receive}
db.mars.insert_one(doc)
# 이름, 주소, 평수의 데이터를 각각의 받는 이름을 지정하고
# db에 각각의 데이터를 이름을 명명해주고
# mars라는 폴더(?)명으로 insert해준다.
function save_order() {
const name = $('#name').val()
const address = $('#address').val()
const size = $('#size').val()
$.ajax({
type: 'POST',
url: '/mars',
data: { name_give: name , address_give:address, size_give:size},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
import requests
from bs4 import BeautifulSoup
url = 'https://movie.naver.com/movie/bi/mi/basic.naver?code=191597'
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')
사이트의 개발자도구 에서 elements를 보면 head에 <meta~~~>로 우성된 코드들을 볼수있습니다.
거기에서 meta태그의 property가 og:title인 것을 가지고오자
거기서 content값만 추출해줘
이런식으로 씁니다.