使用Python将基于pdf的网页下载为pdf

时间:2019-04-22 00:49:11

标签: python python-3.x pdf web

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.

1 个答案:

答案 0 :(得分:3)

对请求包的基本使用将在这里为您提供帮助。 (这只是将结果分块而已。)

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/

相关问题