【问题标题】:Downloading a pdf based webpage as pdf using Python使用 Python 将基于 pdf 的网页下载为 pdf
【发布时间】:2019-09-11 06:27:59
【问题描述】:

Here 提供了一种将网页下载为 pdf 的方法,该方法有效。

但是,我感兴趣的网站本身也显示一个pdf,所以这个方法不起作用。例如,this 页面。这些网址有什么特定的吗?

当我使用上面分享的帖子时,我收到以下错误:

OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
Error: Failed loading page http://curia.europa.eu/juris/showPdf.jsf;jsessionid=CAE85693A88870E357F61ED4344FD7E9?text=&docid=62809&pageIndex=0&doclang=EN&mode=lst&dir=&occ=first&part=1&cid=2878455 (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1, due to unknown error.

【问题讨论】:

    标签: python python-3.x pdf web


    【解决方案1】:

    requests 包的或多或少的基本用法将在这里帮助您。 (这只是对结果进行分块有点花哨。)

    import requests
    outpath = './out.pdf'
    url = r"""http://curia.europa.eu/juris/showPdf.jsf;jsessionid=03B8AD93D8D1B1FBB33A15FDA3774709?text=&docid=62809&pageIndex=0&doclang=EN&mode=lst&dir=&occ=first&part=1&cid=2874259"""
    r = requests.get(url, stream=True)
    if r.status_code == 200:
        with open(outpath, 'wb') as f:
            for chunk in r.iter_content(1024):
                f.write(chunk)
    

    有关请求的更多乐趣,请参阅:https://2.python-requests.org//en/master/

    【讨论】:

    • 是的,而且运行速度非常快!我在一分钟内迭代了 200 多页。谢谢!
    猜你喜欢
    • 2023-03-28
    • 2014-01-05
    • 2012-04-04
    • 1970-01-01
    • 2016-08-17
    • 2016-01-03
    • 1970-01-01
    • 2019-08-10
    • 2018-01-24
    相关资源
    最近更新 更多