Home crawling-task-7
Post
Cancel

crawling-task-7

api를 이용해서 기사 정렬 페이지 만들기

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import requests
from flask import Flask, render_template, request

base_url = "http://hn.algolia.com/api/v1"

# This URL gets the newest stories.
new = f"{base_url}/search_by_date?tags=story"

# This URL gets the most popular stories
popular = f"{base_url}/search?tags=story"

# This function makes the URL to get the detail of a storie by id.
# Heres the documentation: https://hn.algolia.com/api
def make_detail_url(id):
  return f"{base_url}/items/{id}"

db = {}
app = Flask("DayNine")

def get_info(url):
  datas = requests.get(url).json()['hits']
  info = []  
  for data in datas:
    title = data['title']
    url = data['url']
    points = data['points']
    author = data['author']
    num_comments = data['num_comments']
    obj_id = data['objectID']

    info.append({
      "title": title, 
      "url" : url, 
      "points" : points, 
      "author" : author, 
      "num_comments" : num_comments, 
      "id" : obj_id
      }) 
  return info

def get_comments(url):
  data = requests.get(url).json()
  comms = data['children']
  info = []
  comments = []
  for comm in comms:
    author = comm['author']
    comment = comm['text']
    comments.append({"author":author, "comment":comment})

  title = data["title"]
  author = data["author"]
  url = data["url"]
  points = data["points"]
  info.append({"title":title, "author":author, "url":url, "points":points})
  return info, comments

@ app.route("/")
def home():
  query_info = request.args.to_dict()
  # /?order_by=popular
  # {'order_by': 'popular'}

  if query_info.get('order_by') == None:
    query = 'popular'
  else:
    query = query_info['order_by']
  
  if query == "popular":
    exist = db.get("popular")
    if not exist:
      exist = get_info(popular)
      db["popular"] = exist
  else :
    exist = db.get("new")
    if not exist:
      exist = get_info(new)
      db["new"] = exist
  return render_template("index.html",info = exist,toggle = query)

@ app.route("/<id>")
def comments(id):
  # receive id and make detail url
  url = make_detail_url(id)
  info,comments = get_comments(url)
  return render_template("detail.html",info = info,comments = comments)


app.run(host="0.0.0.0")

templates

  • index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>

<head>
  <link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
</head>

<body>
  <header>
    <h1>Nomad News</h1>
    <div>
      Order by:
      &#123;% if toggle == 'popular' %&#125;
        <strong>Popular</strong>
        <a href="/?order_by=new">New</a>
      &#123;% else %&#125;
        <a href="/?order_by=popular">Popular</a>
        <strong>New</strong>
      &#123;% endif %&#125;
    </div>
  </header>
  <main>
    &#123;% for i in info %&#125;
      <div>
        <div>
          <a href="/&#123;&#123;i.id&#125;&#125;">
            <h3>&#123;&#123;i.title&#125;&#125;</h3>
          </a>
          (
          <a href="&#123;&#123;i.url&#125;&#125;" target="_blanl">&#123;&#123;i.url&#125;&#125;</a>
          )
        </div>
        <div>
          &#123;&#123;i.points&#125;&#125; points | By: &#123;&#123;i.author&#125;&#125; | &#123;&#123;i.num_comments&#125;&#125; comments
        </div>
      </div>
      <hr>   
    &#123;% endfor %&#125;
  </main>
</body>

</html>
  • detail.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html>

<head>
  <link href="https://andybrewer.github.io/mvp/mvp.css" rel="stylesheet"></link>
</head>

<body>
</body>
  <header>
    <h1>&#123;&#123;info[0].title&#125;&#125;</h1>
    <div>
      &#123;&#123;info[0].points&#125;&#125; points | By &#123;&#123;info[0].author&#125;&#125; | <a href="&#123;&#123;info[0].url&#125;&#125;">&#123;&#123;info[0].url&#125;&#125;</a>
    </div>
  </header>
  <main>
    &#123;% for com in comments %&#125;
      <div>
        &#123;%if com.author==None %&#125;
          <p>deleted</p>
        &#123;% else %&#125;
          <strong>&#123;&#123;com.author&#125;&#125; : </strong>
          <div> &#123;&#123;com.comment|safe&#125;&#125; </div>
        &#123;% endif %&#125;
      </div>
      <hr>
    &#123;% endfor %&#125;
  </main>
</html>

메모

  • 파이썬 문법부터 flask 사용까지 처음이라 매우 헤맸다

  • 어려웠던 부분

    • api 사용 하기(무슨 의미인지 파악 하기 까지)
    • 가져온 정보를 fake db에 저장할 때의 구조 생각하기
    • route 만드는 논리
    • html에 데이터를 가져가 사용하는 부분
This post is licensed under CC BY 4.0 by the author.