最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)抓取HTML網(wǎng)頁并以PDF文件形式保存的方法

 更新時間:2018年05月08日 10:37:11   作者:Limerence  
這篇文章主要介紹了Python實現(xiàn)抓取HTML網(wǎng)頁并以PDF文件形式保存的方法,結(jié)合實例形式分析了PyPDF2模塊的安裝及Python抓取HTML頁面并基于PyPDF2模塊生成pdf文件的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)抓取HTML網(wǎng)頁并以PDF文件形式保存的方法。分享給大家供大家參考,具體如下:

一、前言

今天介紹將HTML網(wǎng)頁抓取下來,然后以PDF保存,廢話不多說直接進入教程。

今天的例子以廖雪峰老師的Python教程網(wǎng)站為例:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

二、準備工作

1. PyPDF2的安裝使用(用來合并PDF):

PyPDF2版本:1.25.1

https://pypi.python.org/pypi/PyPDF2/1.25.1

https://github.com/mstamy2/PyPDF2

安裝:

pip install PyPDF2

使用示例:

from PyPDF2 import PdfFileMerger
merger = PdfFileMerger()
input1 = open("hql_1_20.pdf", "rb")
input2 = open("hql_21_40.pdf", "rb")
merger.append(input1)
merger.append(input2)
# Write to an output PDF document
output = open("hql_all.pdf", "wb")
merger.write(output)

2. requests、beautifulsoup 是爬蟲兩大神器,reuqests 用于網(wǎng)絡(luò)請求,beautifusoup 用于操作 html 數(shù)據(jù)。有了這兩把梭子,干起活來利索。scrapy 這樣的爬蟲框架我們就不用了,這樣的小程序派上它有點殺雞用牛刀的意思。此外,既然是把 html 文件轉(zhuǎn)為 pdf,那么也要有相應(yīng)的庫支持, wkhtmltopdf 就是一個非常的工具,它可以用適用于多平臺的 html 到 pdf 的轉(zhuǎn)換,pdfkit 是 wkhtmltopdf 的Python封裝包。首先安裝好下面的依賴包

pip install requests
pip install beautifulsoup4
pip install pdfkit

3. 安裝 wkhtmltopdf

Windows平臺直接在 http://wkhtmltopdf.org/downloads.html 下載穩(wěn)定版的 wkhtmltopdf 進行安裝,安裝完成之后把該程序的執(zhí)行路徑加入到系統(tǒng)環(huán)境 $PATH 變量中,否則 pdfkit 找不到 wkhtmltopdf 就出現(xiàn)錯誤 “No wkhtmltopdf executable found”。Ubuntu 和 CentOS 可以直接用命令行進行安裝

$ sudo apt-get install wkhtmltopdf # ubuntu
$ sudo yum intsall wkhtmltopdf   # centos

三、數(shù)據(jù)準備

1. 獲取每篇文章的url

def get_url_list():
  """
  獲取所有URL目錄列表
  :return:
  """
  response = requests.get("http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000")
  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_all("li"):
    url = "http://www.liaoxuefeng.com" + li.a.get('href')
    urls.append(url)
  return urls

2. 通過文章url用模板保存每篇文章的HTML文件

html模板:

html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>
<body>
{content}
</body>
</html>
"""

進行保存:

def parse_url_to_html(url, name):
  """
  解析URL,返回HTML內(nèi)容
  :param url:解析的url
  :param name: 保存的html文件名
  :return: html
  """
  try:
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    # 正文
    body = soup.find_all(class_="x-wiki-content")[0]
    # 標題
    title = soup.find('h4').get_text()
    # 標題加入到正文的最前面,居中顯示
    center_tag = soup.new_tag("center")
    title_tag = soup.new_tag('h1')
    title_tag.string = title
    center_tag.insert(1, title_tag)
    body.insert(1, center_tag)
    html = str(body)
    # body中的img標簽的src相對路徑的改成絕對路徑
    pattern = "(<img .*?src=\")(.*?)(\")"
    def func(m):
      if not m.group(3).startswith("http"):
        rtn = m.group(1) + "http://www.liaoxuefeng.com" + m.group(2) + m.group(3)
        return rtn
      else:
        return m.group(1)+m.group(2)+m.group(3)
    html = re.compile(pattern).sub(func, html)
    html = html_template.format(content=html)
    html = html.encode("utf-8")
    with open(name, 'wb') as f:
      f.write(html)
    return name
  except Exception as e:
    logging.error("解析錯誤", exc_info=True)

3. 把html轉(zhuǎn)換成pdf

def save_pdf(htmls, file_name):
  """
  把所有html文件保存到pdf文件
  :param htmls: html文件列表
  :param file_name: pdf文件名
  :return:
  """
  options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'custom-header': [
      ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
      ('cookie-name1', 'cookie-value1'),
      ('cookie-name2', 'cookie-value2'),
    ],
    'outline-depth': 10,
  }
  pdfkit.from_file(htmls, file_name, options=options)

4. 把轉(zhuǎn)換好的單個PDF合并為一個PDF

merger = PdfFileMerger()
for pdf in pdfs:
  merger.append(open(pdf,'rb'))
  print u"合并完成第"+str(i)+'個pdf'+pdf

完整源碼:

# coding=utf-8
import os
import re
import time
import logging
import pdfkit
import requests
from bs4 import BeautifulSoup
from PyPDF2 import PdfFileMerger
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>
<body>
{content}
</body>
</html>
"""
def parse_url_to_html(url, name):
  """
  解析URL,返回HTML內(nèi)容
  :param url:解析的url
  :param name: 保存的html文件名
  :return: html
  """
  try:
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    # 正文
    body = soup.find_all(class_="x-wiki-content")[0]
    # 標題
    title = soup.find('h4').get_text()
    # 標題加入到正文的最前面,居中顯示
    center_tag = soup.new_tag("center")
    title_tag = soup.new_tag('h1')
    title_tag.string = title
    center_tag.insert(1, title_tag)
    body.insert(1, center_tag)
    html = str(body)
    # body中的img標簽的src相對路徑的改成絕對路徑
    pattern = "(<img .*?src=\")(.*?)(\")"
    def func(m):
      if not m.group(3).startswith("http"):
        rtn = m.group(1) + "http://www.liaoxuefeng.com" + m.group(2) + m.group(3)
        return rtn
      else:
        return m.group(1)+m.group(2)+m.group(3)
    html = re.compile(pattern).sub(func, html)
    html = html_template.format(content=html)
    html = html.encode("utf-8")
    with open(name, 'wb') as f:
      f.write(html)
    return name
  except Exception as e:
    logging.error("解析錯誤", exc_info=True)
def get_url_list():
  """
  獲取所有URL目錄列表
  :return:
  """
  response = requests.get("http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000")
  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_all("li"):
    url = "http://www.liaoxuefeng.com" + li.a.get('href')
    urls.append(url)
  return urls
def save_pdf(htmls, file_name):
  """
  把所有html文件保存到pdf文件
  :param htmls: html文件列表
  :param file_name: pdf文件名
  :return:
  """
  options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'custom-header': [
      ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
      ('cookie-name1', 'cookie-value1'),
      ('cookie-name2', 'cookie-value2'),
    ],
    'outline-depth': 10,
  }
  pdfkit.from_file(htmls, file_name, options=options)
def main():
  start = time.time()
  file_name = u"liaoxuefeng_Python3_tutorial"
  urls = get_url_list()
  for index, url in enumerate(urls):
   parse_url_to_html(url, str(index) + ".html")
  htmls =[]
  pdfs =[]
  for i in range(0,124):
    htmls.append(str(i)+'.html')
    pdfs.append(file_name+str(i)+'.pdf')
    save_pdf(str(i)+'.html', file_name+str(i)+'.pdf')
    print u"轉(zhuǎn)換完成第"+str(i)+'個html'
  merger = PdfFileMerger()
  for pdf in pdfs:
    merger.append(open(pdf,'rb'))
    print u"合并完成第"+str(i)+'個pdf'+pdf
  output = open(u"廖雪峰Python_all.pdf", "wb")
  merger.write(output)
  print u"輸出PDF成功!"
  for html in htmls:
    os.remove(html)
    print u"刪除臨時文件"+html
  for pdf in pdfs:
    os.remove(pdf)
    print u"刪除臨時文件"+pdf
  total_time = time.time() - start
  print(u"總共耗時:%f 秒" % total_time)
if __name__ == '__main__':
  main()

更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • ubuntu系統(tǒng)下使用pm2設(shè)置nodejs開機自啟動的方法

    ubuntu系統(tǒng)下使用pm2設(shè)置nodejs開機自啟動的方法

    今天小編就為大家分享一篇ubuntu系統(tǒng)下使用pm2設(shè)置nodejs開機自啟動的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python實現(xiàn)自動更換ip的方法

    python實現(xiàn)自動更換ip的方法

    這篇文章主要介紹了python實現(xiàn)自動更換ip的方法,涉及Python針對本機網(wǎng)絡(luò)配置的相關(guān)操作技巧,非常具有實用價值,需要的朋友可以參考下
    2015-05-05
  • Python使用pyforms實現(xiàn)輕松構(gòu)建GUI

    Python使用pyforms實現(xiàn)輕松構(gòu)建GUI

    pyforms是一個基于Python的GUI框架,它提供了一種簡單而強大的方式來構(gòu)建圖形用戶界面,本文將介紹pyforms的使用場景、優(yōu)勢以及常用的Python代碼案例,需要的小伙伴可以了解下
    2024-01-01
  • python實現(xiàn)庫存商品管理系統(tǒng)

    python實現(xiàn)庫存商品管理系統(tǒng)

    這篇文章主要為大家詳細介紹了python實現(xiàn)庫存商品管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • json-server?如何快速搭建REST?API?服務(wù)器

    json-server?如何快速搭建REST?API?服務(wù)器

    json-server 是一個非常流行的開源工具,用于快速搭建一個完整的 REST API 服務(wù)器,它使用 JSON 文件作為數(shù)據(jù)源,通過簡單的配置即可模擬復雜的服務(wù)器功能,這篇文章主要介紹了json-server如何快速搭建REST API服務(wù)器,需要的朋友可以參考下
    2017-10-10
  • dataframe 按條件替換某一列中的值方法

    dataframe 按條件替換某一列中的值方法

    今天小編就為大家分享一篇dataframe 按條件替換某一列中的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python實現(xiàn)SMTP郵件發(fā)送功能

    python實現(xiàn)SMTP郵件發(fā)送功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)SMTP郵件發(fā)送功能的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • python實現(xiàn)網(wǎng)站的模擬登錄

    python實現(xiàn)網(wǎng)站的模擬登錄

    這篇文章主要介紹了python實現(xiàn)網(wǎng)站的模擬登錄的相關(guān)資料,通過自己構(gòu)造post數(shù)據(jù)來用Python實現(xiàn)登錄過程,需要的朋友可以參考下
    2016-01-01
  • PyQt5打開文件對話框QFileDialog實例代碼

    PyQt5打開文件對話框QFileDialog實例代碼

    這篇文章主要介紹了PyQt5打開文件對話框QFileDialog實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • 簡單學習Python多進程Multiprocessing

    簡單學習Python多進程Multiprocessing

    這篇文章主要和大家一起簡單的學習Python多進程Multiprocessing ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論

祁连县| 山东省| 泰宁县| 靖江市| 德保县| 和龙市| 沽源县| 肃宁县| 通海县| 海晏县| 平遥县| 玉田县| 松阳县| 佛山市| 北碚区| 来凤县| 政和县| 哈尔滨市| 华容县| 万安县| 永康市| SHOW| 沙雅县| 陆良县| 西林县| 南溪县| 郎溪县| 琼结县| 百色市| 峨山| 普格县| 含山县| 邻水| 达拉特旗| 巴南区| 财经| 惠州市| 巴楚县| 平罗县| 龙岩市| 永丰县|