learned by here 把廖雪峰教程转换成 PDF 电子书.You can also craw other websites’ data and then parse to the pdf format.
#-*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import pdfkit
def parse_url_to_html(url):
response = requests.get(url)
soup = BeautifulSoup(response.content,'html.parser')
body = soup.find_all(class_ = 'x-wiki-content')[0]
html = str(body)
print('html content:%s' %(html))
with open('content.html','wb') as f:
f.write(html)
def get_url_list(url):
response = requests.get(url)
#print(response.content)
soup = BeautifulSoup(response.content,'html.parser')
menu_tag = soup.find_all(class_='uk-nav uk-nav-side')[1]
urls = []
for li in menu_tag.find('li'):
temp_url = 'http://www.liaoxuefeng.com' + li.a.get('href')
urls.append(temp_url)
return urls
def save_pdf(htmls):
options = {
'page-size':'Letter',
'encoding':'UTF-8',
'custom-header':[
('Accept-Encoding','gzip')
]
}
pdfkit.from_file(htmls,file_name,options = options)
if __name__ == '__main__':
url = b'https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000'
urls = get_url_list(url)
for u in urls:
parse_url_to_html(u)
file_name = 'python course'
save_pdf('content.html')