본문 바로가기
인공지능/PYTHON

Crawling and Flask

by bibibig_data 2021. 6. 24.

[002-4] Crawling and Flask.txt
0.04MB

 


예제 1 weather

style.css

.naver_weather {
  padding: 20px;
  border: 10px solid blue;
  border-radius: 50px;
}

index.html

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
  <title>My Home Page</title>
</head>
<body>
  <div class="naver_weather">
  <ul>
    {% for i in list %}
    <li>{{ i }}</li>
    {% endfor %}
  </ul>
  </div>
</body>
</html>

app.py

from flask import Flask, render_template
import crawling

app = Flask(__name__)


@app.route('/')
def index():
    naver_weather = crawling.naver_weather()
    return render_template("index.html", list=naver_weather)


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

crawling.py

import requests
from bs4 import BeautifulSoup


def naver_weather():
    html = requests.get('https://search.naver.com/search.naver?query=날씨')
    soup = BeautifulSoup(html.text, 'html.parser')

    find_address     = '현재 위치 : '      + soup.find('span', class_='btn_select').text
    find_currenttemp = '현재 온도: '       + soup.select_one('p.info_temperature > span.todaytemp').text
    find_dust        = '현재 미세먼지: '   + soup.select('dd span.num')[0].text
    find_ultra_dust  = '현재 초미세먼지: ' + soup.select('dd span.num')[1].text
    find_ozone       = '현재 오존지수: '   + soup.select('dd span.num')[2].text

    myList = [find_address, find_currenttemp,
              find_dust, find_ultra_dust, find_ozone]

    return myList


if __name__ == '__main__':
    print(naver_weather())

 


 

예제 2 오늘의 유머

 

index.html

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
  <title>My Home Page</title>
</head>
<body>
  <div class='naver_weather'>
  <h1>지역 날씨</h1>
  <ul>
    {% for i in list1 %}
    <li>{{ i }}</li>
    {% endfor %}
  </ul>
  </div>

  <div class='naver_weather'>
  <h1>오늘의 유머 베오베</h1>
  <ul>
    {% for i in list2 %}
    <li>{{ i }}</li>
    {% endfor %}
  </ul>
  </div>

  <div class='naver_weather'>
  <h1>지역 날씨입니다</h1>
  <ul>
    {% for i in list3 %}
    <li>{{ i }}</li>
    {% endfor %}
  </ul>  
  </div>

</body>
</html>

 

crawling.py

import requests
from bs4 import BeautifulSoup

def naver_weather():
    html = requests.get('https://search.naver.com/search.naver?query=날씨')
    soup = BeautifulSoup(html.text, 'html.parser')

    find_address     = '현재 위치 : '      + soup.find('span', class_='btn_select').text
    find_currenttemp = '현재 온도: '       + soup.select_one('p.info_temperature > span.todaytemp').text
    find_dust        = '현재 미세먼지: '   + soup.select('dd span.num')[0].text
    find_ultra_dust  = '현재 초미세먼지: ' + soup.select('dd span.num')[1].text
    find_ozone       = '현재 오존지수: '   + soup.select('dd span.num')[2].text

    myList = [find_address, find_currenttemp,
              find_dust, find_ultra_dust, find_ozone]

    return myList


def today_humor() :
    resp = requests.get('http://www.todayhumor.co.kr/board/list.php?table=bestofbest')
    soup = BeautifulSoup(resp.text, 'html.parser')

    myList = []

    for i in soup.find_all("td", class_="subject"):
        myList.append(i.text)

    return myList


def clien():
    resp = requests.get('https://www.clien.net/service/recommend')
    soup = BeautifulSoup(resp.text, 'html.parser')

    myList = []

    for i in soup.find_all("span", class_="subject_fixed") :
        myList.append(i.text)

    return myList


if __name__ == '__main__':
    print(naver_weather())
    print(today_humor())
    print(clien())

app.py

from flask import Flask, render_template
import crawling

app = Flask(__name__)

@app.route('/')
def index():
    naver_weather = crawling.naver_weather()
    todayhumor    = crawling.today_humor()
    clien         = crawling.clien()

    return render_template("index.html", list1=naver_weather, list2=todayhumor, list3=clien)


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

 


 

예제 3 ) 하이퍼링크

 

index.html

<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>

<body>

  <div class="naver_weather">
    <h1>지역 날씨입니다</h1>
    <ul>
      {% for i in list1 %}
      <li>{{i}}</li>
      {% endfor %}
    </ul>
  </div><br>

  <div class="naver_weather">
    <h1>오늘의 유머</h1>
    <ul>
      {% for i in range(0, list2_len) %}
      <a href={{list2_href[i]}}>
        <li>{{list2[i]}}</li>
      </a>
      {% endfor %}
    </ul>
  </div><br>

  <div class="naver_weather">
    <h1>클리앙</h1>
    <ul>
      {% for i in range(0, list3_len) %}
      <a href={{list3_href[i]}}>
        <li>{{list3[i]}}</li>
      </a>
      {% endfor %}
    </ul>
  </div>

</body>

</html>

crawling.py

import requests
from bs4 import BeautifulSoup

def naver_weather():
    html = requests.get('https://search.naver.com/search.naver?query=날씨')
    soup = BeautifulSoup(html.text, 'html.parser')

    find_address     = '현재 위치 : '      + soup.find('span', class_='btn_select').text  
    find_currenttemp = '현재 온도: '       + soup.select_one('p.info_temperature > span.todaytemp').text
    find_dust        = '현재 미세먼지: '   + soup.select('dd span.num')[0].text
    find_ultra_dust  = '현재 초미세먼지: ' + soup.select('dd span.num')[1].text
    find_ozone       = '현재 오존지수: '   + soup.select('dd span.num')[2].text

    myList = [find_address, find_currenttemp,
              find_dust, find_ultra_dust, find_ozone]
              
    return myList


def today_humor() :
    resp = requests.get('http://www.todayhumor.co.kr/board/list.php?table=bestofbest')
    soup = BeautifulSoup(resp.text, 'html.parser')

    myList = []
    myList_href = []	

    for i in soup.find_all("td", class_="subject"):
        myList.append(i.text)
        myList_href.append("http://www.todayhumor.co.kr/" + i.find("a")["href"])

    return myList, myList_href
    

def clien():
    resp = requests.get('https://www.clien.net/service/recommend')
    soup = BeautifulSoup(resp.text, 'html.parser')

    myList = []
    myList_href = []

    for i in soup.find_all("span", class_="subject_fixed") :
        myList.append(i.text)

    for i in soup.find_all("a", class_="list_subject") :
        myList_href.append("https://www.clien.net/" + i["href"])

    return myList, myList_href
   
    
if __name__ == '__main__':
    print(naver_weather())
    print(today_humor())
    print(clien())

app.py

from flask import Flask, render_template
import crawling

app = Flask(__name__)

@app.route('/')
def index():
    naver_weather = crawling.naver_weather()
    today_humor, todayhumor_href = crawling.today_humor()
    clien, clien_href = crawling.clien()

    return render_template("index.html",
                           list1 = naver_weather,
                           list2 = today_humor, list2_href = todayhumor_href, list2_len = len(today_humor),
                           list3 = clien, list3_href = clien_href, list3_len = len(clien))

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

 


예제 4 ) Bootstrap

 

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css

 

ctrl + alt + b  -> 난독화

'인공지능 > PYTHON' 카테고리의 다른 글

Crawling and Flask (2)  (0) 2021.06.25
셀레늄  (0) 2021.06.25
크롤링 (2)  (0) 2021.06.23
Flask 2  (0) 2021.06.22
Flask  (0) 2021.06.22