-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
piaotian.py
71 lines (55 loc) · 2.61 KB
/
piaotian.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
# -*- coding: utf-8 -*-
import logging
from lncrawl.core.crawler import Crawler
logger = logging.getLogger(__name__)
novel_search_url = "%smodules/article/search.php"
cover_image_url = "%sfiles/article/image/%s/%s/%ss.jpg"
class PiaoTian(Crawler):
base_url = [
"https://www.piaotian.com",
"https://www.ptwxz.com",
"https://www.piaotia.com"
]
def read_novel_info(self):
# Transform bookinfo page into chapter list page
# https://www.piaotia.com/bookinfo/8/8866.html -> https://www.piaotia.com/html/8/8866/
if self.novel_url.startswith("%sbookinfo/" % self.home_url):
self.novel_url = self.novel_url.replace("/bookinfo/", "/html/").replace(".html", "/")
if self.novel_url.endswith("index.html"):
self.novel_url = self.novel_url.replace("/index.html", "/")
soup = self.get_soup(self.novel_url, encoding="gbk")
self.novel_title = soup.select_one("div.title").text.replace("最新章节", "").strip()
logger.info("Novel title: %s", self.novel_title)
author = soup.select_one("div.list")
author.select_one("a").decompose()
self.novel_author = author.text.replace("作者:", "").strip()
logger.info("Novel author: %s", self.novel_author)
ids = self.novel_url.replace("%shtml/" % self.home_url, "").split("/")
logger.debug(self.home_url)
self.novel_cover = cover_image_url % (self.home_url, ids[0], ids[1], ids[1])
logger.info("Novel cover: %s", self.novel_cover)
for a in soup.select("div.centent ul li a"):
chap_id = len(self.chapters) + 1
vol_id = 1 + len(self.chapters) // 100
if chap_id % 100 == 1:
self.volumes.append({"id": vol_id})
self.chapters.append(
{
"id": chap_id,
"volume": vol_id,
"title": a.text,
"url": self.novel_url + a["href"],
}
)
def download_chapter_body(self, chapter):
headers = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9"}
raw_html = self.get_response(chapter.url, headers=headers)
raw_html.encoding = "gbk"
raw_text = raw_html.text.replace('<script language="javascript">GetFont();</script>', '<div id="content">')
self.last_soup_url = chapter.url
soup = self.make_soup(raw_text)
body = soup.select_one("div#content")
for elem in body.select("h1, script, div, table"):
elem.decompose()
text = self.cleaner.extract_contents(body)
return text