我有一个脚本可以从URL下载PDF,但是它会将它保存到python脚本所在的同一个目录中。我想把它保存到另一个目录
我试过用save_path来改变它,但是它不起作用,但是我也没有发现任何错误
# Import all needed modules and tools
from fileinput import filename
import os
import os.path
from datetime import datetime
import urllib.request
import requests
# Disable SSL and HTTPS Certificate Warnings
import urllib3
urllib3.disable_warnings()
resp = requests.get('url.pdf', verify=False)
response= urllib.request.urlretrieve('url.pdf',
filename = 'civil.pdf')
save_path = "C: my preferred directory"发布于 2022-03-21 00:52:25
您可以直接与filename一起使用它。
urllib.request.urlretrieve('url.pdf',
filename='C:\\you_preferred directory\\civil.pdf')如果你想用requests下载
with open("C:\\you_preferred directory\\civil.pdf", 'wb') as f:
f.write(resp.content)最终,您应该在下载之前更改当前目录。
os.chdir("C:\\you_preferred directory\\")然后您可以使用简短的filename='civil.pdf'和open('civil.pdf', 'wb')
发布于 2022-03-21 00:58:14
已解决,按评论
# Import all needed modules and tools
from fileinput import filename
import os
import os.path
from datetime import datetime
import urllib.request
import requests
# Disable SSL and HTTPS Certificate Warnings
import urllib3
urllib3.disable_warnings()
resp = requests.get('url.org', verify=False)
# Download and name the PDF file from the URL
response= urllib.request.urlretrieve('url.pdf',
filename = 'C:\\ location')
# Save to the preferred directory
with open("C:\\ location", 'wb') as f: f.write(resp.content)https://stackoverflow.com/questions/71551206
复制相似问题