使用pis呈现错误的PDF

2024-09-29 07:35:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试写一些代码来抓取网页。我使用spynner获取html代码并将其传递给pisa

我在运行python代码时没有看到任何错误,但是pdf生成是完全错误的。在

这是我使用的代码-

import os
import sys
import ho.pisa as pisa
import spynner

import logging
class PisaNullHandler(logging.Handler):
    def emit(self, record):
        pass


url = 'http://www.google.com'

br = spynner.Browser()

br.load(url)

pathToWrite = './google.html.pdf'

htmlCode = br.html

pdfFile = file(pathToWrite, "wb+")
try:
    logging.getLogger("ho.pisa").addHandler(PisaNullHandler())
    pdfStatus = pisa.CreatePDF(htmlCode.encode("utf-8"), pdfFile, encoding="utf8" )
    if not pdfStatus.err:
        pdfFile.flush()
    else:
        print 'Failed with error ' + pdfStatus.error
except Exception as e:
    print 'pdf creation failed with error ' + str(e)

我试图从spynner保存html并通过xhtml2pdf运行它。我得到以下错误-

^{pr2}$

Tags: 代码brimportpdflogginghtmlas错误