网络编程
位置:首页>> 网络编程>> Python编程>> Python实现抓取HTML网页并以PDF文件形式保存的方法

Python实现抓取HTML网页并以PDF文件形式保存的方法

作者:Limerence  发布时间:2022-12-15 22:56:30 

标签:Python,HTML,PDF文件

本文实例讲述了Python实现抓取HTML网页并以PDF文件形式保存的方法。分享给大家供大家参考,具体如下:

一、前言

今天介绍将HTML网页抓取下来,然后以PDF保存,废话不多说直接进入教程。

今天的例子以廖雪峰老师的Python教程网站为例: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 用于网络请求,beautifusoup 用于操作 html 数据。有了这两把梭子,干起活来利索。scrapy 这样的爬虫框架我们就不用了,这样的小程序派上它有点杀鸡用牛刀的意思。此外,既然是把 html 文件转为 pdf,那么也要有相应的库支持, wkhtmltopdf 就是一个非常的工具,它可以用适用于多平台的 html 到 pdf 的转换,pdfkit 是 wkhtmltopdf 的Python封装包。首先安装好下面的依赖包


pip install requests
pip install beautifulsoup4
pip install pdfkit

3. 安装 wkhtmltopdf

Windows平台直接在 http://wkhtmltopdf.org/downloads.html 下载稳定版的 wkhtmltopdf 进行安装,安装完成之后把该程序的执行路径加入到系统环境 $PATH 变量中,否则 pdfkit 找不到 wkhtmltopdf 就出现错误 “No wkhtmltopdf executable found”。Ubuntu 和 CentOS 可以直接用命令行进行安装


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

三、数据准备

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内容
 :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转换成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. 把转换好的单个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内容
 :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"转换完成第"+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相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

来源:https://blog.csdn.net/hubaoquanu/article/details/66973149

0
投稿

猜你喜欢

  • 1.删除所有的目录,只保留datasharebin2.删除BIN下面除以下三个文件之外的所有文件:libmysql.dll(MYSQL5中的
  • Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"
  • jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多javascript高手加入其team,包括来自德国的J&
  • MySQL服务器有几个影响其操作的参数(变量)。如果缺省的参数值不合适,可以将其修改为对服务器运行环境更合适的值。例如,如果您有大量的内存,
  • 用在产品内容展示的页面上,给产品图片加上提示工具条,鼠标移动图片上即可看到效果,效果还是不错的:带黑白效果和放大图片效果(by misshj
  • 简介如果你经常网上冲浪,这样参差不齐的多栏布局,是不是很眼熟啊?类似的布局,似乎一夜之间出现在国内外大大小小的网站上,比如 Pinteres
  •  首先这个是7米当时问我一个问题,不过可惜我不知道怎么解决,后来知道解决方法很简单,只是添加了一个margin-top:-1em就
  • XML有很多相关的技术,将这些技术结合起来,才能充分发挥XML的强大功能。这些技术包括:Xlink与Xpointer(设置XML的超链接)、
  • SQL Server 2008已经发布,我们可以看到它在各方面都有了显著的进步,这些让人侧目之处和失望之处都有可能极大地影响大型企业的采购意
  • 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求访问url地址,
  • 事先说明哦,这不是一篇关于Python异常的全面介绍的文章,这只是在学习Python异常后的一篇笔记式的记录和小结性质的文章。什么?你还不知
  • 运维平台导入数据这一功能实在是太重要了,我敢说在没有建自己的cmdb平台前,大多数公司管理服务器信息肯定是表格,用表格最麻烦的就是有点更新就
  • 古巴比伦王颁布了汉摩拉比法典,刻在黑色的玄武岩,距今已经三千七百多年,你在橱窗前…熟悉吧?没错,这就是周董的爱在西元前歌词。前不久工作不是很
  • { hide_text } CSS文字隐藏总结报告最近整理的一份CSS文字隐藏的demo,总结了几种方法,希望得出一种最完美的方案放进自己的
  • 简单的Python代码:用户登录注册利用业余时间,写了一个用户进行登录注册的代码,非常简单。主要实现的功能是:1、可以进行用户登录,在用户进
  • 引文之前将PHP反序列化的基础知识讲了一遍,不知道大家学习的怎么样了,今天给大家带来PHP反序列化的进阶知识:PHAR反序列化,也是之前本人
  • 目录一、环境准备二、问题分析三、spider四、item五、setting六、pipelines七、middlewares八、使用jupyt
  • 你知道(X)HTML中最多余的标签中是什么吗?在我看来就是这个<a>标签,不错,就是每个网站使用最多的超级链接标签<a&g
  • 导读:由于banner一般用于专题类网站,在门户网站的二级页面,用户进来之前,在首页已经对主题有一定的了解和认识,所以banner的作用是在
  • 1、XML 是什么?XML仅仅是一种数据存放格式,这种格式是一种文本(虽然XML规范中也提供了存放二进制数据的解决方案)。事实上有很多文本格
手机版 网络编程 asp之家 www.aspxhome.com