python 爬虫练习二, 爬取python标准库为pdf

来源:互联网 发布:cf驱动辅助源码 编辑:程序博客网 时间:2024/06/05 15:17

爬取python标准库

想要把Python的标准库文档趴下来,试过直接存成html,但是简单的存储css的样式等都会丢失,遂想存为pdf。

需要一个工具pdfkit,结合之前的selenium爬下来。

首先需要

pip install pdfkit
# coding:utf-8import urllibfrom urllib import requestimport os,timefrom os import pathfrom selenium import webdriverimport pdfkitimport re#去掉尖括号之间的内容def transname(name):    pattern = '<.*?>'    res = re.compile(pattern).sub("",name)    return resurl_root = 'https://docs.python.org/3/library/'url_index = url_root + 'index.html'result_dir = path.join(os.getcwd(),'result')if not path.exists(result_dir):    os.makedirs(result_dir)#pdfkit.from_url(url_index,path.join(result_dir,'index.pdf'))driver = webdriver.PhantomJS()# driver = webdriver.Firefox()driver.get(url_index)html_index = driver.page_sourcepattern = '<li class="toctree-l[12]"><a class="reference internal" href="(.+?)">(.+?)</a>'res = re.compile(pattern,re.S).findall(html_index)print(res)ct = 0amt = len(res)for i,x in enumerate(res):    if i<127:        continue    addr = url_root + x[0]    if re.compile('.*\.html$').match(addr):        name = re.compile(os.sep).sub(r'-',x[1])     #去掉系统转换的符号,防止误把路径分割了        name = path.join(result_dir,name+'.pdf')        name = transname(name)        ct = ct + 1        print(ct,'/',i+1,'/',amt,addr,name)        pdfkit.from_url(addr,name)    else:        amt = amt - 1

结果
这里写图片描述

原创粉丝点击