
홈페이지에서 새로고침 할 때마다 데이터가 계속 쌓입니다! 파이썬 실행을 안해도 계속 쌓이는데 어떻게하면될까요?
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://hoisohee:dladnrgus77@cluster0.yfexp.mongodb.net/Cluster0?retryWrites=true&w=majority', tlsCAFile=ca)
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/bsnews", methods=["POST"])
def news_post():
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,
'comment':comment_receive
}
db.bsnews.insert_one(doc)
return jsonify({'msg':'저장완료!'})
@app.route("/bsnews", methods=["GET"])
def news_get():
sport_list = list(db.bsnews.find({}, {'_id': False}))
return jsonify({'bsnews':sport_list})
@app.route("/bsnewsrank", methods=["GET"])
def newsrank_get():
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://www.koreabaseball.com/TeamRank/TeamRank.aspx', headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
# 1등
teamrank = ranks.select_one('tr:nth-child(1) > td:nth-child(1)').text
teamtitle = ranks.select_one('td:nth-child(2)').text
teampoint = ranks.select_one(' td:nth-child(7)').text
teamgame = ranks.select_one("td:nth-child(8)").text
doc = {
'teamrank': teamrank,
'teamtitle': teamtitle,
'teamgame': teamgame,
'teampoint': teampoint
}
print("doc:", doc)
db.bsrank.insert_one(doc)
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank2 = ranks.select_one('tr:nth-child(2) > td:nth-child(1)').text
teamtitle2 = ranks.select_one('tr:nth-child(2) > td:nth-child(2)').text
teampoint2 = ranks.select_one('tr:nth-child(2) > td:nth-child(7)').text
teamgame2 = ranks.select_one("tr:nth-child(2) > td:nth-child(8)").text
doc = {
'teamrank': teamrank2,
'teamtitle': teamtitle2,
'teamgame': teamgame2,
'teampoint': teampoint2
}
db.bsrank.insert_one(doc)
# 3등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank3 = ranks.select_one('tr:nth-child(3) > td:nth-child(1)').text
teamtitle3 = ranks.select_one('tr:nth-child(3) > td:nth-child(2)').text
teampoint3 = ranks.select_one('tr:nth-child(3) > td:nth-child(7)').text
teamgame3 = ranks.select_one("tr:nth-child(3) > td:nth-child(8)").text
doc = {
'teamrank': teamrank3,
'teamtitle': teamtitle3,
'teamgame': teamgame3,
'teampoint': teampoint3
}
db.bsrank.insert_one(doc)
# 4등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank4 = ranks.select_one('tr:nth-child(4) > td:nth-child(1)').text
teamtitle4 = ranks.select_one('tr:nth-child(4) > td:nth-child(2)').text
teampoint4 = ranks.select_one('tr:nth-child(4) > td:nth-child(7)').text
teamgame4 = ranks.select_one("tr:nth-child(4) > td:nth-child(8)").text
doc = {
'teamrank': teamrank4,
'teamtitle': teamtitle4,
'teamgame': teamgame4,
'teampoint': teampoint4
}
db.bsrank.insert_one(doc)
# 5등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank5 = ranks.select_one('tr:nth-child(5) > td:nth-child(1)').text
teamtitle5 = ranks.select_one('tr:nth-child(5) > td:nth-child(2)').text
teampoint5 = ranks.select_one('tr:nth-child(5) > td:nth-child(7)').text
teamgame5 = ranks.select_one("tr:nth-child(5) > td:nth-child(8)").text
doc = {
'teamrank': teamrank5,
'teamtitle': teamtitle5,
'teamgame': teamgame5,
'teampoint': teampoint5
}
db.bsrank.insert_one(doc)
# 6등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank6 = ranks.select_one('tr:nth-child(6) > td:nth-child(1)').text
teamtitle6 = ranks.select_one('tr:nth-child(6) > td:nth-child(2)').text
teampoint6 = ranks.select_one('tr:nth-child(6) > td:nth-child(7)').text
teamgame6 = ranks.select_one("tr:nth-child(6) > td:nth-child(8)").text
doc = {
'teamrank': teamrank6,
'teamtitle': teamtitle6,
'teamgame': teamgame6,
'teampoint': teampoint6
}
db.bsrank.insert_one(doc)
# 7등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank7 = ranks.select_one('tr:nth-child(7) > td:nth-child(1)').text
teamtitle7 = ranks.select_one('tr:nth-child(7) > td:nth-child(2)').text
teampoint7 = ranks.select_one('tr:nth-child(7) > td:nth-child(7)').text
teamgame7 = ranks.select_one("tr:nth-child(7) > td:nth-child(8)").text
doc = {
'teamrank': teamrank7,
'teamtitle': teamtitle7,
'teamgame': teamgame7,
'teampoint': teampoint7
}
db.bsrank.insert_one(doc)
# 8등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank8 = ranks.select_one('tr:nth-child(8) > td:nth-child(1)').text
teamtitle8 = ranks.select_one('tr:nth-child(8) > td:nth-child(2)').text
teampoint8 = ranks.select_one('tr:nth-child(8) > td:nth-child(7)').text
teamgame8 = ranks.select_one("tr:nth-child(8) > td:nth-child(8)").text
doc = {
'teamrank': teamrank8,
'teamtitle': teamtitle8,
'teamgame': teamgame8,
'teampoint': teampoint8
}
db.bsrank.insert_one(doc)
# 9등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank9 = ranks.select_one('tr:nth-child(9) > td:nth-child(1)').text
teamtitle9 = ranks.select_one('tr:nth-child(9) > td:nth-child(2)').text
teampoint9 = ranks.select_one('tr:nth-child(9) > td:nth-child(7)').text
teamgame9 = ranks.select_one("tr:nth-child(9) > td:nth-child(8)").text
doc = {
'teamrank': teamrank9,
'teamtitle': teamtitle9,
'teamgame': teamgame9,
'teampoint': teampoint9
}
db.bsrank.insert_one(doc)
# 10등
rank = soup.select('#cphContents_cphContents_cphContents_udpRecord > table > tbody ')
for ranks in rank:
teamrank10 = ranks.select_one('tr:nth-child(10) > td:nth-child(1)').text
teamtitle10 = ranks.select_one('tr:nth-child(10) > td:nth-child(2)').text
teampoint10 = ranks.select_one('tr:nth-child(10) > td:nth-child(7)').text
teamgame10 = ranks.select_one("tr:nth-child(10) > td:nth-child(8)").text
doc = {
'teamrank': teamrank10,
'teamtitle': teamtitle10,
'teamgame': teamgame10,
'teampoint': teampoint10
}
db.bsrank.insert_one(doc)
rank_list = list(db.bsrank.find({}, {'_id': False}))
print("rank_list",rank_list)
return jsonify({'bsnewsrank': rank_list})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
