我正在尝试在Python下面的链接中下载PDF文件。
我试图下载它,但无法打开已保存的文件
我的PDF查看器给出了“源格式不是PDF格式”
有人能告诉我有什么问题吗?
import urllib2
def main():
url = "https://www.osapublishing.org/view_article.cfm?gotourl=https%3A%2F%2Fwww%2Eosapublishing%2Eorg%2FDirectPDFAccess%2F42C574A0-ABB6-FD11-777A24C1C4C5ADEF_274099%2Foe-21-22-27371%2Epdf%3Fda%3D1%26id%3D274099%26seq%3D0%26mobile%3Dno&org="
download_file("example", url)
def download_file(file_name, download_url):
response = urllib2.urlopen(download_url)
file = open(file_name + ".pdf", 'wb')
file.write(response.read())
file.close()
print("Completed")
if __name__ == "__main__":
main()
答案 0 :(得分:2)
您的网址不是指向PDF的链接,而是指向包含PDF的HTML框架。请改用直接网址:
url = "http://www.osapublishing.org/DirectPDFAccess/42C574A0-ABB6-FD11-777A24C1C4C5ADEF_274099/oe-21-22-27371.pdf?da=1&id=274099&seq=0&mobile=no"
您可以通过查看原始链接的HTML源来获取PDF文件的来源。