-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
70 lines (60 loc) · 2.41 KB
/
utils.py
File metadata and controls
70 lines (60 loc) · 2.41 KB
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
from collections import namedtuple
from urllib.parse import urlparse, parse_qs
import requests
from bs4 import BeautifulSoup
from episode import Episode
Webtoon = namedtuple('Webtoon', ['title_id', 'img_url', 'title'])
# Episode = namedtuple('Episode', ['no', 'img_url', 'title', 'rating', 'created_date'])
webtoon_yumi = 651673
webtoon_p = 696617
def get_webtoon_episode_list(webtoon, page=1):
"""
특정 page의 episode 리스트를 리턴하도록 리팩토링
:param webtoon: 웹툰 고유 ID
:param page: 가져오려는 Episode list 페이지
:return: list(Episode)
"""
# webtoon_url을 기반으로 특정 웹툰의 리스트페이지 내용을 가져와 soup객체에 할당
webtoon_list_url = 'http://comic.naver.com/webtoon/list.nhn'
params = {
'titleId': webtoon.title_id,
'page': page,
}
response = requests.get(webtoon_list_url, params=params)
soup = BeautifulSoup(response.text, 'lxml')
# 필요한 데이터들 (img_url, title, rating, created_date)추출
episode_list = list()
webtoon_table = soup.select_one('table.viewList')
tr_list = webtoon_table.find_all('tr', recursive=False)
for tr in tr_list:
td_list = tr.find_all('td')
if len(td_list) < 4:
continue
td_thumbnail = td_list[0]
td_title = td_list[1]
td_rating = td_list[2]
td_created_date = td_list[3]
# Episode고유의 no
url_episode = td_thumbnail.a.get('href')
parse_result = urlparse(url_episode)
queryset = parse_qs(parse_result.query)
no = queryset['no'][0]
# td_thumbnail에 해당하는 Tag의 첫 번째 a tag의 첫 번째 img태그의 'src'속성값
url_thumbnail = td_thumbnail.a.img.get('src')
# td_title tag의 내용을 좌우여백 잘라냄
title = td_title.get_text(strip=True)
# td_rating내의 strong태그내의 내용을 좌우여백 잘라냄
rating = td_rating.strong.get_text(strip=True)
# td_title과 같음
created_date = td_created_date.get_text(strip=True)
# Episode형 namedtuple객체 생성, episode_list에 추가
episode = Episode(
webtoon=webtoon,
no=no,
url_thumbnail=url_thumbnail,
title=title,
rating=rating,
created_date=created_date
)
episode_list.append(episode)
return episode_list