Craw the data of the web page and parse to pdf

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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值