• 使用pdfkit转换url为pdf遇到的问题


    1. 转换完后未显示图片

    原因:wkhtmltopdf 模块默认情况下禁用本地文件访问

    解决方案:pdfkit.from_string的执行语句加入参数  options={'encoding': "UTF-8", 'enable-local-file-access': True}

    如:
    pdfkit.from_string(html, pdf_filename, options={'encoding': "UTF-8", 'enable-local-file-access': True},
    configuration=config)

    2. 运行报错:

    File "D:\python\python3.8.1\lib\site-packages\wechatsogou\structuring.py", line 474, in get_article_detail
    qqmusic = content_text.find_all('qqmusic') or []
    AttributeError: 'NoneType' object has no attribute 'find_all'

    原因:文章的只有标题,没有内容,导致找不到属性

    解决方案:从报错可以看出是wechatsogou库tructuring.py文件的get_article_detail函数报错,于是我们直接找到该函数,修改文件的代码,改为正文无内容时不执行,直接返回空值

    3. 运行报错:

    File "D:\python\python3.8.1\lib\site-packages\pdfkit\pdfkit.py", line 155, in handle_error
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
    OSError: wkhtmltopdf reported an error:
    Exit with code 1 due to network error: ContentNotFoundError

    原因:找不到文件,这个是由于执行下面语句时:

    pdfkit.from_string(html, pdf_filename, options={'encoding': "UTF-8", 'enable-local-file-access': True},
    configuration=config)

    传入的的html包含外部文件,找不到时就会报错,我看了下我传入的这篇文章,确实有个投票的外部链接。

    解决方案:修改html文件,把包含的外部文件相关的标签删除,可以使用bs4库,先找到对应标签,并使用compose删除,举例:

     

    wechatsogou库tructuring.py文件的get_article_detail函数仅对文件中的音乐食品,图片,以及iframe做了处理,如果文章中有其他地方引用了外部链接都可能报错,需要自己删除下

    4. 运行报错:

    File "D:\python\python3.8.1\lib\site-packages\pdfkit\pdfkit.py", line 158, in handle_error
    raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, error_msg))
    OSError: wkhtmltopdf exited with non-zero code 1. error:
    QPainter::begin(): Returned false
    Exit with code 1, due to unknown error.

    原因:输入源存在错误,可以检查下参数是否有问题,我这里报错是因为传的文件路径不存在,导致报错。

    解决方案:修改有问题的参数

    5. 运行报错:

    File "D:\python\python3.8.1\lib\site-packages\wechatsogou\structuring.py", line 615, in get_article_detail_bak
    content_html = re.findall(js_content, content_html)[0][0]
    IndexError: list index out of range

    原因:报这个错有两个可能,一个可能是list的下标超出范围,一个可能是list是空的,没有一个元素。所以我们找到wechatsogou库tructuring.py文件的get_article_detail_bak函数,排查发现是执行music.parent.decompose()时,把父标签删除了,而恰好这里的父标签是整个文章,所以导致整个html文件被清空了。

    解决方案:修改代码,music.parent.decompose()改为music.decompose()

  • 相关阅读:
    debug
    whlie and for
    while and for 2
    用鸿蒙开发AI应用(七)触摸屏控制LED
    animation动画组件在鸿蒙中的应用&鸿蒙的定时函数和js的动画效果
    2020技术征文大赛获奖名单公示
    HarmonyOS三方件开发指南(8)——RoundedImage
    从微信小程序到鸿蒙js开发【02】——数据绑定&tabBar&swiper
    从微信小程序到鸿蒙js开发【01】——环境搭建&flex布局
    HarmonyOS三方件开发指南(7)——compress组件
  • 原文地址:https://www.cnblogs.com/leslie12956/p/16308804.html
Copyright © 2020-2023  润新知