데이터 공부를 기록하는 공간

[crawling,flask] 프로젝트 1 - 인기글 불러오는 페이지 만들기 본문

STUDY/MLOPS

[crawling,flask] 프로젝트 1 - 인기글 불러오는 페이지 만들기

BOTTLE6 2021. 12. 18. 08:20

강의 출처 : 인프런 - 실시간트렌드홈페이지개발(크롤링+flask)

강의에서는 다음, 오늘, 클리앙인데 다음이 크롤링이 잘 되지 않아 나머지만 실행함 

 

<결과물>


<app.py> 

from flask import Flask, render_template
app = Flask(__name__)

import crawling

@app.route('/')
def hello():
  
    list_clien, list_clien_href = crawling.clien()
    list_today, list_today_href = crawling.today()

    return render_template("index.html", 
                                clien = list_clien,
                                today = list_today,
                                clien_href = list_clien_href,
                                today_href = list_today_href,
                                clien_len = len(list_clien),
                                today_len = len(list_today))

# 경로설정
@app.route('/about')
def about():
    return "여기는 어바웃입니다"

if __name__ == '__main__':
    app.run()

- crawling.py 파일로 크롤링을 따로 꺼내두어서, import 하여 크롤링 실행함

- crawling.clien()

- crawling.today()


<crawling.py>

# 크롤링 라이브러리 import
import requests
from bs4 import BeautifulSoup

def clien():
    req = requests.get("http://clien.net/service/")
    soup = BeautifulSoup(req.text, 'html.parser')
    list_clien = [] 
    list_clien_href = []   
    for i in soup.find_all("a", attrs={"class": "list_subject"}):
        list_clien.append(i.find("span", class_='subject').text.strip())
        list_clien_href.append("https://www.clien.net" + i['href'])
    return list_clien, list_clien_href

def today():
    url = "http://www.todayhumor.co.kr/"
    req = requests.get(url)
    soup = BeautifulSoup(req.text, 'html.parser')
    list_today = []    
    list_today_href = []
    for i in soup.find_all("span", attrs={"class": "subject"}):
        list_today.append(i.text.strip())
        list_today_href.append(url + i.find('a')['href'])
    return list_today, list_today_href

- list_clien, list_today는 인기글 제목 저장한 리스트

- list_clien_href, list_today_href는 인기글 주소를 저장한 리스트


<index.html>

app.py가 있는 폴더 하위에 templates라는 폴더를 만들고 그 아래 만들어주기

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <title>Title</title>
    <!-- 3.CSS파일 스타일 불러오기-->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/index_style.css') }}">
</head>
<body>
    <div>
        여기는 html영역입니다.
        <!-- 1.링크 만들어보기-->
        <a href = "/about">어바웃 페이지로 이동</a> 
    </div>

    <!-- 2.파이썬문법형태로 플라스크 문법 치기-->
    <div class='today_content'></div>
        <h1> 오늘의 인기글 목록  </h1>

        <ul>
            {% for i in range(0, today_len) %}
                
            <a href="{{today_href[i]}}"><li>{{ today[i] }}</li></a>
            
            {% endfor %}
        </ul>  
        </ul>     
    </div>

    <div class='clien_content'></div>
        <h1> 클리앙 인기글 목록  </h1>        
        <ul>
            {% for i in range(0, clien_len) %}
                
            <a href="{{clien_href[i]}}"><li>{{ clien[i] }}</li></a>
            
            {% endfor %}
        </ul>   
    </div>
</body>    
</html>

<index_style_css.css>

app.py가 있는 폴더에 "static" 하위폴더 만들고 그 아래 "css" 하위 폴더를 만들고 저장

.clien_content {

    padding: 20px;
    border: 10px solid green;
    border-radius: 50px;
    margin-top : 20px;

}

- 무슨 이유에서인지 적용은 안되었음. 차차 해결해 나가면 될 듯 함

'STUDY > MLOPS' 카테고리의 다른 글

전력수급 실시간 crawling  (0) 2021.12.19
[crawling, flask] 프로젝트2 - 뉴스 및 네이버쇼핑 크롤링 페이지  (0) 2021.12.19
ubuntu에 아나콘다 설치하기  (0) 2021.12.07
DOCKER 설치 오류 관련  (0) 2021.12.06
MLOPS  (0) 2021.11.06
Comments