我正在尝试下载位于 PHP 服务器上的 PDF(一些可以是 word 文件,很少)。看来在服务器上,PDF 的编号从 1 逐渐增加到 14000。可以使用以下链接下载 PDF:http://ppmoe.dot.ca.gov/des/oe/awards/bidsum/dl.php?id=X ,其中 X 是 [1, 14000] 范围内的数字。我将以下代码用于 X = 200,然后我可以循环遍历所有 [1, 14000] 值以将所有文件保存在特定文件夹中:
import requests
url = "http://ppmoe.dot.ca.gov/des/oe/awards/bidsum/dl.php?id=200"
s = requests.Session()
response = s.get(url)
with open("file200.pdf", "w") as f:
f.write(response.content)
f.close()
但它返回以下错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: write() argument must be str, not bytes
我不确定我们是否可以使用 python 下载这些文件,而且 PHP 对我来说并不熟悉。谢谢!
最佳答案
您需要将 b
添加到参数中,以便它将数据作为二进制数据写入文件(response.content
包含字节,而不是字符串):
with open("file200.pdf", "wb") as f:
f.write(response.content)
f.close()
关于python - 使用python从php服务器下载pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74879561/