마음의 소리 웹툰의 회차정보와 제목 가져오기
|
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 |
from bs4 import BeautifulSoup import requests def get_html(url) : _html = "" resp = requests.get(url) if resp.status_code == 200: _html = resp.text return _html URL = "http://comic.naver.com/webtoon/list.nhn?titleId=20853&weekday=tue&page=1" html = get_html(URL) soup = BeautifulSoup(html, 'html.parser') webtoon_list = list() webtoon_area = soup.find("table", {"class": "viewList"}).find_all("td", {"class": "title"}) #파싱한 URL에서 table 태크에서 class 가 viewList 인 html 객체를 가져오고, 그중에서도 td 채크이면서 class 가 title 인 객체를 가져옴 for webtoon_index in webtoon_area: info_soup = webtoon_index.find("a") #a태크만 가져옴 _url = info_soup["href"] #url 가져오기 위해 href속성의 내용만 가져옴 _text = info_soup.text.split(".") #html코드에서 텍스트만 가져오고 . 구분자로 나눔 (회차와 제목) _title = "" _num = _text[0] if len(_text) > 1: _title = _text[1].strip() #_title에 제목을 넣기 위해서 webtoon_list.append((_num, _title, _url, )) for print_ in webtoon_list: print(print_) |