網頁製作pdf

前言

沉迷農藥,抖音,總歸是不好的。在要吃苦的年紀裏選擇安逸,那麼老了一定會後悔,埋怨年輕的時候不知道努力的自己。於是又找出了塵封已久的kindle,打算好好看看書,給自己充充電。

Amazon上好書要錢,readfree上充斥着文學類的書,思來想去,找到自己喜歡的內容,自己製作電子書來看豈不是一個更好的選擇。而且內容完全由自己來定,把每天蒐集到的不錯的網頁製成PDF發到kindle上,下班回去就可以拿出kindle好好看了。

說搞就搞,網上搜了搜大致的實現,對比了各種語言的實現方式,發現了wkhtmltopdfpdfkit最爲舒適,人生苦短,那就它們吧。

安裝wkhtmltopdf

Mac用戶可以參考下面的鏈接進行安裝;其他平臺類似
http://macappstore.org/wkhtmltopdf/

  • Press Command+Space and type Terminal and press enter/return key.
    Run in Terminal app:
  • ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null ; brew install caskroom/cask/brew-cask 2> /dev/null
    and press enter/return key.
  • If the screen prompts you to enter a password, please enter your Mac’s user password to continue. When you type the password, it won’t be displayed on screen, but the system would accept it. So just type your password and press ENTER/RETURN key. Then wait for the command to finish.
    Run:
brew cask install wkhtmltopdf

pdfkit

網上輪子很多,這裏不過多敘述,放上我參考的一篇博客園博主的優秀文章,可以說很是簡潔明瞭。

pip install pdfkit

https://www.cnblogs.com/linzenews/p/6972192.html

實現

#coding: utf8
import requests
import pdfkit
from bs4 import BeautifulSoup

# 生成HTML的文件時,需要將圖片路徑替換爲本地的絕對路徑,生成PDF之後再進行刪除操作
def gethtml(url):
    headers = {
        "Referer": "https://blog.csdn.net/marksinoberg",
        "Host": "blog.csdn.net",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"
    }
    resp = requests.get("https://blog.csdn.net/Marksinoberg/article/details/82700073", headers=headers)
    soup = BeautifulSoup(resp.text, "html.parser")
    content = soup.find("div", {"id": "article_content"})
    return soup.title.string, str(content)

def savepdf(filename, title):
    options = {
        "page-size": "Letter",
        "encoding": "UTF-8",
        "custom-header": [
            ("Accept-Encoding", "gzip")
        ]
    }
    pdfkit.from_file(filename, "./{}.pdf".format(title), options=options)

if __name__ == "__main__":
    title, html = gethtml('')
    # print(html)
    print(title)
    exit()
    with open("./test.html", "w") as file:
         file.write(html)
         file.close()
    savepdf("./test.html", title)

執行腳本

$ python testpdf.py
Go+PHP實現敏感詞檢測 - CSDN博客
Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done                                                                      
$

查看生成效果
blog生成pdf效果

轉推到kindle

在Amazon的官網上,去設置一個推送書籍的郵箱,然後通過stmp協議即可實現將PDF推送到kindle上。之前寫過一個命令行發送郵件的小工具,想用的可以去GitHub上直接下載。
https://github.com/guoruibiao/worktools/tree/master/mailer

下班前整理下鏈接,放到服務器上去生成下PDF,轉推到kindle,估計還沒到家就已經完事了,凡此種種,腳本加上crontab都能搞定。晚上洗漱完畢,躺到牀上,就可以美美的閱讀了,比起刷抖音,打遊戲,總歸是不算荒廢了時光,辜負了青春。

推送到kindle上的效果

遇到的問題

示例中用到的博客鏈接裏面有圖片,如果圖片服務器沒有爬蟲限制的話,那麼基本上沒什麼問題。相反,像CSDN這種,圖牀會有一個Referer的驗證,因此需要先將文本中的圖片先下載出來,再去替換HTML文件中對應的位置(正如參考的文章中說的,需要做成本地絕對路徑)。

理論上來講,有一點反反爬蟲對策就夠了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章