-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
novel35.py
86 lines (69 loc) · 2.85 KB
/
novel35.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
# -*- coding: utf-8 -*-
import logging
from urllib.parse import quote
from bs4 import ResultSet, Tag
from lncrawl.core.crawler import Crawler
from lncrawl.core.exeptions import LNException
from lncrawl.models import Chapter, SearchResult
logger = logging.getLogger(__name__)
class Novel35Crawler(Crawler):
base_url = ["https://novel35.com/"]
def initialize(self) -> None:
self.cleaner.bad_css.update(
['div[align="left"]', 'img[src*="proxy?container=focus"]']
)
def search_novel(self, query):
soup = self.get_soup(f"{self.home_url}search?keyword={quote(query)}")
results = []
for div in soup.select("#list-page .archive .list-truyen > .row"):
a = div.select_one(".truyen-title a")
info = div.select_one(".text-info a .chapter-text")
results.append(
SearchResult(
title=a.text.strip(),
url=self.absolute_url(a["href"]),
info=info.text.strip() if isinstance(info, Tag) else "",
)
)
return results
def read_novel_info(self):
self.novel_url = self.novel_url.split("?")[0].strip("/")
soup = self.get_soup(self.novel_url)
title_tag = soup.select_one("h1.title")
if not isinstance(title_tag, Tag):
raise LNException("No title found")
self.novel_title = title_tag.text.strip()
image_tag = soup.select_one(".info-holder .book img")
if isinstance(image_tag, Tag):
self.novel_cover = self.absolute_url(image_tag["src"])
logger.info("Novel cover: %s", self.novel_cover)
self.novel_author = ", ".join(
[
a.text.strip()
for a in soup.select(".info-holder .info a[href^='/author/']")
if isinstance(a, Tag)
]
)
logger.info("Novel author: %s", self.novel_author)
pagination_link = soup.select("#list-chapter ul.pagination a:not([rel])")
page_count = (
int(pagination_link[-1].get_text())
if isinstance(pagination_link, ResultSet)
else 1
)
logger.info("Chapter list pages: %d" % page_count)
for page in range(1, page_count + 1):
url = f"{self.novel_url}?page={page}"
soup = self.get_soup(url)
for a in soup.select("ul.list-chapter li a"):
self.chapters.append(
Chapter(
id=len(self.chapters) + 1,
title=a["title"].strip(),
url=self.absolute_url(a["href"]),
)
)
def download_chapter_body(self, chapter):
soup = self.get_soup(chapter["url"])
contents = soup.select_one("div#chapter-content")
return self.cleaner.extract_contents(contents)