我如何使用 python 通过 https 下载 pdf 文件
How do i download pdf file over https with python
我正在编写一个python脚本,它将根据URL中给出的格式将pdf文件保存在本地。例如。
https://Hostname/saveReport/file_name.pdf #saves the content in PDF file.
我正在通过 python 脚本打开此 URL:
import webbrowser
webbrowser.open("https://Hostname/saveReport/file_name.pdf")
url 包含大量图片和文字。 打开此 URL 后,我想使用 python 脚本以 pdf 格式保存文件。
这是我到目前为止所做的。
代码 1:
import requests
url="https://Hostname/saveReport/file_name.pdf" #Note: It's https
r = requests.get(url, auth=('usrname', 'password'), verify=False)
file = open("file_name.pdf", 'w')
file.write(r.read())
file.close()
代码 2:
import urllib2
import ssl
url="https://Hostname/saveReport/file_name.pdf"
context = ssl._create_unverified_context()
response = urllib2.urlopen(url, context=context) #How should i pass authorization details here?
html = response.read()
在上面的代码中,我得到:urllib2.HTTPError:HTTP 错误 401:未经授权
如果我使用代码 2,我如何传递授权详细信息?
您可以尝试类似的方法:
import requests
response = requests.get('https://websitewithfile.com/file.pdf',verify=False, auth=('user', 'pass'))
with open('file.pdf','w') as fout:
fout.write(response.read()):
我认为这行得通
import requests
import shutil
url="https://Hostname/saveReport/file_name.pdf" #Note: It's https
r = requests.get(url, auth=('usrname', 'password'), verify=False,stream=True)
r.raw.decode_content = True
with open("file_name.pdf", 'wb') as f:
shutil.copyfileobj(r.raw, f)
您可以这样做的一种方法是:
import urllib3
urllib3.disable_warnings()
url = r"https://websitewithfile.com/file.pdf"
fileName = r"file.pdf"
with urllib3.PoolManager() as http:
r = http.request('GET', url)
with open(fileName, 'wb') as fout:
fout.write(r.data)
对于某些文件 - 至少 tar 个存档(或什至所有其他文件)您可以使用 pip:
import sys
from subprocess import call, run, PIPE
url = "https://blabla.bla/foo.tar.gz"
call([sys.executable, "-m", "pip", "download", url], stdout=PIPE, stderr=PIPE)
但是您应该通过其他方式确认下载是否成功,因为 pip 会对任何不包含 setup.py 的存档文件引发错误,因此 stderr=PIPE(或者您可以确定下载是否成功通过解析子进程错误消息成功)。
我正在编写一个python脚本,它将根据URL中给出的格式将pdf文件保存在本地。例如。
https://Hostname/saveReport/file_name.pdf #saves the content in PDF file.
我正在通过 python 脚本打开此 URL:
import webbrowser
webbrowser.open("https://Hostname/saveReport/file_name.pdf")
url 包含大量图片和文字。 打开此 URL 后,我想使用 python 脚本以 pdf 格式保存文件。
这是我到目前为止所做的。
代码 1:
import requests
url="https://Hostname/saveReport/file_name.pdf" #Note: It's https
r = requests.get(url, auth=('usrname', 'password'), verify=False)
file = open("file_name.pdf", 'w')
file.write(r.read())
file.close()
代码 2:
import urllib2
import ssl
url="https://Hostname/saveReport/file_name.pdf"
context = ssl._create_unverified_context()
response = urllib2.urlopen(url, context=context) #How should i pass authorization details here?
html = response.read()
在上面的代码中,我得到:urllib2.HTTPError:HTTP 错误 401:未经授权
如果我使用代码 2,我如何传递授权详细信息?
您可以尝试类似的方法:
import requests
response = requests.get('https://websitewithfile.com/file.pdf',verify=False, auth=('user', 'pass'))
with open('file.pdf','w') as fout:
fout.write(response.read()):
我认为这行得通
import requests
import shutil
url="https://Hostname/saveReport/file_name.pdf" #Note: It's https
r = requests.get(url, auth=('usrname', 'password'), verify=False,stream=True)
r.raw.decode_content = True
with open("file_name.pdf", 'wb') as f:
shutil.copyfileobj(r.raw, f)
您可以这样做的一种方法是:
import urllib3
urllib3.disable_warnings()
url = r"https://websitewithfile.com/file.pdf"
fileName = r"file.pdf"
with urllib3.PoolManager() as http:
r = http.request('GET', url)
with open(fileName, 'wb') as fout:
fout.write(r.data)
对于某些文件 - 至少 tar 个存档(或什至所有其他文件)您可以使用 pip:
import sys
from subprocess import call, run, PIPE
url = "https://blabla.bla/foo.tar.gz"
call([sys.executable, "-m", "pip", "download", url], stdout=PIPE, stderr=PIPE)
但是您应该通过其他方式确认下载是否成功,因为 pip 会对任何不包含 setup.py 的存档文件引发错误,因此 stderr=PIPE(或者您可以确定下载是否成功通过解析子进程错误消息成功)。