以下是一个Python的示例代码,用于保存浏览器中的PDF文件到磁盘:
import requests
def save_pdf_from_browser(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as file:
file.write(response.content)
# 示例使用方法:
url = 'https://example.com/path/to/pdf.pdf'
save_path = 'C:/path/to/save/pdf.pdf'
save_pdf_from_browser(url, save_path)
这里使用了requests
库来发送GET请求并获取PDF文件的内容。然后,使用open
函数将内容写入到指定的保存路径中。请确保已经安装了requests
库,可以使用pip install requests
进行安装。
你需要将url
替换为实际的PDF文件的URL地址,并将save_path
替换为你希望保存文件的路径和文件名。执行这段代码后,PDF文件将被保存到指定的路径中。