-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler-for-linux-vbird-org.py
68 lines (56 loc) · 1.92 KB
/
crawler-for-linux-vbird-org.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
import os
from urllib import (request, error)
from pyquery import PyQuery as pq
import pdfkit
from PyPDF2 import PdfFileMerger
def get_html(url):
html = None
try:
html = request.urlopen(url, timeout=5)
except error.URLError as e:
print(e.reason)
return html
def get_catalog(html):
doc = pq(html.read().decode('utf-8'))
block = doc('.block1').eq(0)
catalog = [[a.text(), []] for a in block.items('a')]
for i in range(len(catalog)):
block = doc('.block1').eq(i + 1)
for tr in block.items('tr'):
chapter = tr('td').eq(0).children('a').text().replace('\n', '')
link = tr('td').eq(0).children('a').attr('href')
catalog[i][1].append([chapter, link])
return catalog
def down_as_pdf(catalog):
i = 0
for part in catalog:
for chapter in part[1]:
print(('/pdf/ch%02d') % i)
pdfkit.from_url(chapter[1], ('./pdf/ch%02d.pdf') % i)
i = i + 1
def get_pdf_list(path):
pdf_list = []
for file in os.listdir(path):
filename = os.path.join(path, file)
if '.pdf' in filename and not os.path.isdir(filename):
pdf_list.append(filename)
return pdf_list
def merge_pdf(catalog, pdf_list, output_path):
bookmark_list = []
i = 0
for part in catalog:
for chapter in part[1]:
bookmark_list.append(('第%02d章 ' % i) + chapter[0])
i = i + 1
merger = PdfFileMerger()
for i in range(len(pdf_list)):
merger.append(open(pdf_list[i], 'rb'), bookmark=bookmark_list[i])
with open(output_path, 'wb') as fout:
merger.write(fout)
if __name__ == '__main__':
html = get_html('http://cn.linux.vbird.org/linux_basic/linux_basic.php')
catalog = get_catalog(html)
down_as_pdf(catalog)
pdf_list = get_pdf_list('./pdf')
pdf_list.sort()
merge_pdf(catalog, pdf_list, '鸟哥的Linux私房菜:基础学习篇.pdf')