1. 程式人生 > >把知乎丁香醫生的文章及回答轉pdf

把知乎丁香醫生的文章及回答轉pdf

之前爬取了1800多萬的知乎使用者,因而想生成pdf方便儲存和閱讀,正好試試wkhtmltopdf+pdfkit。
1:pdfkit是wkhtmltopdf的python封裝,因此需要先下載安裝wkhtmltopdf,版本wkhtmltopdf-0.12.2.4_mingw-w64,下載地址http://download.csdn.net/download/cainiaowuzui/10024376%20%E2%80%9Cwkhtmltopdf-0.12.2.4_mingw-w64%E2%80%9D 
2:使用pip install pdfkit安裝pdfkit(版本0.6.1)
這裡我選擇把丁香醫生的文章+回答按獲贊數從高到低排序生成pdf。
pdfkit預設是生成帶目錄書籤的pdf.由於內容儲存在mongo資料庫中,因此我先從mongo資料庫中選取出來再通過pdfkit.from_string()生成pdf。
主要知識點:python,pdfkit,mongo,html,知乎爬取 
過程中遇到以下問題:
       1. 中文亂碼,引數中須指定編碼options={'encoding': 'utf-8'}
       2. pdf目錄無法自動生成,經過試驗,採用把字串補成html格式的字串,且輸出檔案指定為out.pdf,可以解決問題。
       3. 個別回答資料庫中沒有對應的提問標題,因此去知乎爬取對應的提問,有興趣爬取知乎的朋友可以參考。
import pymongo
import os 
from bson.objectid import ObjectId
import wkhtmltopdf
import pdfkit
import requests
import json
client=pymongo.MongoClient(host="127.0.0.1")
db=client["zhihu"]
wkhtmltopdf_path='F:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'#wkhtmltopdf的安裝目錄
def printpdf(url_token)
:
cursor=db['article'].find({"url_token":url_token}) article=list(cursor) article.sort(key=lambda k :k["voteup_count"],reverse=True)#按文章獲讚的總數排序 formatstr='''<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" class=" no-touch"> <head> <meta charset="utf-8" /> <title>{title}</title> </head> <body> <h1 align='center'>{title}</h1>{content}</body> </html> '''
#補成html格式的字串 pdfstr='' for art in article: pdfstr+=formatstr.format(title=art['title'],content=art['content']) cursor=db['answer'].find({"url_token":url_token}) article=list(cursor) article.sort(key=lambda k :k["voteup_count"],reverse=True) headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36', 'authorization':'這裡填你的知乎令牌'} for art in article: question_id=art["question_id"] cid=str(question_id) _id = ObjectId(bytes(cid.ljust(12),encoding='utf-8')) question=db['question'].find_one({"_id":_id})#通過問題id找問題的標題 if question: title=question["title"] else: #如果在question表中找不到相應的question,那麼去知乎網址爬取,嗯由於不多,這裡僅僅單執行緒 print("can not find question :%d,now try to get" % question_id) url='https://www.zhihu.com/api/v4/questions/{id}'.format(id=question_id) r=requests.get(url=url,headers=headers) try: results = json.loads(r.text) results['url_token']='' results['_id']=_id db['question'].update({'_id': _id}, {'$set': results}, True)#儲存到mongo資料庫 title=results['title'] print("save question %d ok" % question_id) except Exception as e: print(e) title=cid pdfstr+= formatstr.format(title=title,content=art['content']) pdfstr.replace('\"','"') print("prepare to pdf") filetree="G:\\ebook\\zhihu\\article\\"+url_token+"\\" if os.path.exists(filetree): pass else: os.mkdir(filetree) file=filetree+"out.pdf" #多次測試,如不指定為out.pdf不會生成pdf目錄,原因不清楚,如有朋友知道麻煩告知 options={'encoding': 'utf-8', 'outline-depth': 1}#指定utf-8編碼,防止中文亂碼 config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path) pdfkit.from_string(pdfstr,file,options=options,configuration=config)

最後,上一張效果圖
丁香醫生文章+回答

最後附上wkhtmltopdf的中文引數詳解
wkhtmltopdf [OPTIONS]… [More input files]
常規選項
–allow 允許載入從指定的資料夾中的檔案或檔案(可重複)
–book* 設定一會列印一本書的時候,通常設定的選項
–collate 列印多份副本時整理
–cookie 設定一個額外的cookie(可重複)
–cookie-jar 讀取和寫入的Cookie,並在提供的cookie jar檔案
–copies 影印列印成pdf檔案數(預設為1)
–cover* 使用HTML檔案作為封面。它會帶頁首和頁尾的TOC之前插入
–custom-header 設定一個附加的HTTP頭(可重複)
–debug-javascript 顯示的javascript除錯輸出
–default-header* 新增一個預設的頭部,與頁面的左邊的名稱,頁面數到右邊,例如: –header-left ‘[webpage]’ –header-right ‘[page]/[toPage]’ –header-line
–disable-external-links* 禁止生成連結到遠端網頁
–disable-internal-links* 禁止使用本地連結
–disable-javascript 禁止讓網頁執行JavaScript
–disable-pdf-compression* 禁止在PDF物件使用無失真壓縮
–disable-smart-shrinking* 禁止使用WebKit的智慧戰略收縮,使畫素/ DPI比沒有不變
–disallow-local-file-access 禁止允許轉換的本地檔案讀取其他本地檔案,除非explecitily允許用 –allow
–dpi 顯式更改DPI(這對基於X11的系統沒有任何影響)
–enable-plugins 啟用已安裝的外掛(如Flash
–encoding 設定預設的文字編碼
–extended-help 顯示更廣泛的幫助,詳細介紹了不常見的命令開關
–forms* 開啟HTML表單欄位轉換為PDF表單域
–grayscale PDF格式將在灰階產生
–help Display help
–htmldoc 輸出程式HTML幫助
–ignore-load-errors 忽略claimes載入過程中已經遇到了一個錯誤頁面
–lowquality 產生低品質的PDF/ PS。有用縮小結果文件的空間
–manpage 輸出程式手冊頁
–margin-bottom 設定頁面下邊距 (default 10mm)
–margin-left 將左邊頁邊距 (default 10mm)
–margin-right 設定頁面右邊距 (default 10mm)
–margin-top 設定頁面上邊距 (default 10mm)
–minimum-font-size 最小字型大小 (default 5)
–no-background 不列印背景
–orientation 設定方向為橫向或縱向
–page-height 頁面高度 (default unit millimeter)
–page-offset* 設定起始頁碼 (default 1)
–page-size 設定紙張大小: A4, Letter, etc.
–page-width 頁面寬度 (default unit millimeter)
–password HTTP驗證密碼
–post Add an additional post field (repeatable)
–post-file Post an aditional file (repeatable)
–print-media-type* 使用的列印介質型別,而不是螢幕
–proxy 使用代理
–quiet Be less verbose
–read-args-from-stdin 讀取標準輸入的命令列引數
–readme 輸出程式自述
–redirect-delay 等待幾毫秒為JS-重定向(default 200)
–replace* 替換名稱,值的頁首和頁尾(可重複)
–stop-slow-scripts 停止執行緩慢的JavaScripts
–title 生成的PDF檔案的標題(第一個文件的標題使用,如果沒有指定)
–toc* 插入的內容的表中的檔案的開頭
–use-xserver* 使用X伺服器(一些外掛和其他的東西沒有X11可能無法正常工作)
–user-style-sheet 指定使用者的樣式表,載入在每一頁中
–username HTTP認證的使用者名稱
–version 輸出版本資訊退出
–zoom 使用這個縮放因子 (default 1)

頁首和頁尾選項
–header-center* (設定在中心位置的頁首內容)
–header-font-name* (default Arial) (設定頁首的字型名稱)
–header-font-size* (設定頁首的字型大小)
–header-html* (新增一個HTML頁首,後面是網址)
–header-left* (左對齊的頁首文字)
–header-line* (顯示一條線在頁首下)
–header-right* (右對齊頁首文字)
–header-spacing* (設定頁首和內容的距離,預設0)
–footer-center* (設定在中心位置的頁尾內容)
–footer-font-name* (設定頁尾的字型名稱)
–footer-font-size* (設定頁尾的字型大小default 11)
–footer-html* (新增一個HTML頁尾,後面是網址)
–footer-left* (左對齊的頁尾文字)
–footer-line* 顯示一條線在頁尾內容上)
–footer-right* (右對齊頁尾文字)
–footer-spacing* (設定頁尾和內容的距離)
./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf
./wkhtmltopdf –header-center ‘報表’ –header-line –margin-top 2cm –header-line http://192.168.212.139/oma/ oma.pdf
表內容選項中
–toc-depth* Set the depth of the toc (default 3)
–toc-disable-back-links* Do not link from section header to toc
–toc-disable-links* Do not link from toc to sections
–toc-font-name* Set the font used for the toc (default Arial)
–toc-header-font-name* The font of the toc header (if unset use –toc-font-name)
–toc-header-font-size* The font size of the toc header (default 15)
–toc-header-text* The header text of the toc (default Table Of Contents)
–toc-l1-font-size* Set the font size on level 1 of the toc (default 12)
–toc-l1-indentation* Set indentation on level 1 of the toc (default 0)
–toc-l2-font-size* Set the font size on level 2 of the toc (default 10)
–toc-l2-indentation* Set indentation on level 2 of the toc (default 20)
–toc-l3-font-size* Set the font size on level 3 of the toc (default 8)
–toc-l3-indentation* Set indentation on level 3 of the toc (default 40)
–toc-l4-font-size* Set the font size on level 4 of the toc (default 6)
–toc-l4-indentation* Set indentation on level 4 of the toc (default 60)
–toc-l5-font-size* Set the font size on level 5 of the toc (default 4)
–toc-l5-indentation* Set indentation on level 5 of the toc (default 80)
–toc-l6-font-size* Set the font size on level 6 of the toc (default 2)
–toc-l6-indentation* Set indentation on level 6 of the toc (default 100)
–toc-l7-font-size* Set the font size on level 7 of the toc (default 0)
–toc-l7-indentation* Set indentation on level 7 of the toc (default 120)
–toc-no-dots* Do not use dots, in the toc
輪廓選項
–dump-outline 轉儲目錄到一個檔案
–outline 顯示目錄(文章中h1,h2來定)
–outline-depth 設定目錄的深度(預設為4)
頁尾和頁首
* [page] 由當前正在列印的頁的數目代替
* [frompage] 由要列印的第一頁的數量取代
* [topage] 由最後一頁要列印的數量取代
* [webpage] 通過正在列印的頁面的URL替換
* [section] 由當前節的名稱替換
* [subsection] 由當前小節的名稱替換
* [date] 由當前日期系統的本地格式取代
* [time] 由當前時間,系統的本地格式取代
./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf
./wkhtmltopdf –header-center ‘報表’ –outline –header-line –margin-top 2cm –header-line http://www.hao123.com/ hao123.pdf
./wkhtmltopdf –header-left ‘[webpage]’ –footer-center ‘測試([page]/[toPage])’ http://www.baidu.com baidu.pdf