【问题标题】:How to ignore the error "Exit with code 1 due to network error: ContentNotFoundError" in python?如何忽略python中的错误“由于网络错误而退出代码1:ContentNotFoundError”?
【发布时间】:2020-12-06 07:49:06
【问题描述】:

我编写了将html页面转换为pdf的脚本,该脚本生成了一个pdf但它给出了错误“Exit with code 1 due to network error: ContentNotFoundError”,我想我们可以忽略它使用--load-error-handling ignore,但我不知道如何在 python 中使用它。我正在使用 Anaconda Spyder Python 3.7。 我的脚本如下:

import pdfkit

try:
    import urlparse
    from urllib import urlencode
except: # For Python 3
    import urllib.parse as urlparse
    from urllib.parse import urlencode

# url = "http://stackoverflow.com/search?q=question"
# params = {'lang':'en','tag':'python'}

photoid = 3
seaid_destination = 4    
search = "merge_recog"

url = "http://localhost:8080/" + search + "?"
params = {'photoid':photoid,'seaid_destination':seaid_destination}

url_parse = urlparse.urlparse(url)
query = url_parse.query            #retrieve query item
url_dict = dict(urlparse.parse_qsl(query))     #convert the list to dict
url_dict.update(params)                  #add params to dict
url_new_query = urlencode(url_dict)            #convert it in "percent-encoded"
url_parse = url_parse._replace(query=url_new_query)   #replace old query with new query
new_url = urlparse.urlunparse(url_parse)        #construct new url
print(new_url)

config = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf")
pdfkit.from_url(new_url, search+"_"+str(photoid)+"_"+str(seaid_destination)+'.pdf',
                configuration = config)

【问题讨论】:

    标签: python-3.x error-handling wkhtmltopdf


    【解决方案1】:

    同样的问题。正如您所注意到的,根据他们的documentation,唯一可用的配置选项是 wkhtmltopdfmeta_tag_prefix

    不幸的是,我最终做了:

    try:
        pdfkit.from_file(path, new_path)
    except OSError as e:
        if 'Done' not in str(e):
            raise e
    

    假设 pdfkit 无法生成输出文件时不会出现“完成”一词,我确保异常字符串中存在“完成”一词。 相当糟糕的解决方案。我希望他们能尽快修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多