Skip to main content
 首页 » 编程设计

python之使用 Python 自动下载嵌入式 PDF 文件

2025年05月04日16del

我正在编写一个 Python 脚本来自动下载网站上托管的一些 pdf 页面(来自公共(public)领域的作品)。 不幸的是,单个 pdf 页面嵌入在框架中,当我使用以下内容时:

import time, urllib 
for n in range(21,63): 
    time.sleep(2) 
    pdfPath="http://babel.hathitrust.org/cgi/imgsrv/download/pdf?id=wu.89038803698;orient=0;size=100;seq=%s;attachment=0"%(str(n)) 
    pdfName="Housner_"+str(n)+".pdf" 
    f = open(pdfName, 'w') 
    f.write(urllib.urlopen(pdfPath).read()) 
    f.close() 
    time.sleep(2) 

下载的文件实际上是空白的,Adobe 显示错误,例如未找到无效图像、嵌入字体等。

任何人都可以建议我如何改进这个脚本,以便下载的 PDF 不会出错/损坏。

谢谢。

请您参考如下方法:

将 'w' 替换为 open(pdfName, 'w') 中的 'wb'