python-3.x - 尝试在 python 中下载多个文件并报告是否成功

标签 python-3.x

所以我正在尝试从我抓取的网站下载文件(图像和文档)。我必须将这些下载到特定文件夹。到目前为止,我有:

images = re.findall("/([^/]+\.(?:jpg|gif|png))", html)
output = open("output.txt","a+")
output.write("\n" + f"[+] {len(images)} Images Found:" + "\n")
for images in images:
    output.write(images + "\n")
    output.write("Beginning file download with urllib2..." + "\n")
    imageurl = "images"
    urllib.request.urlretrieve(url, "/downloads")

如何使文件名与网站上特定文件类型等的文件名保持一致?

这只是处理图像的代码片段。

最佳答案

您可以将输出文件名放入urllib.request.urlretrieve

images = re.findall("/([^/]+\.(?:jpg|gif|png))", html)
output = open("output.txt","a+")
output.write("\n" + f"[+] {len(images)} Images Found:" + "\n")
for images in images:
    output.write(images + "\n")
    output.write("Beginning file download with urllib2..." + "\n")
    imageurl = "images"
    urllib.request.urlretrieve(url, "/downloads" + imagename)

[您只需将变量设置为图像名称即可。例如image.png]

希望能帮到你

关于python-3.x - 尝试在 python 中下载多个文件并报告是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47578628/

相关文章:

python - 列表理解和遍历列表

python - 如何重命名路径名?

python-3.x - python 使用服务帐户 json 文件推送 GCP 云存储文件

python-3.x - Tensorflow中是否可以采用张量的模式?

python - 如何设置 header (用户代理)、检索网页、捕获重定向和接受 cookie?

python - 在 Python 中将原始推文字符串转换为 JSON 对象

python - 列表理解中的奇怪拆包

python-3.x - 函数 'contourArea' 中的 OpenCV(4.0.0) 断言失败

python - 无法在Python3、Ubuntu14.04中使用pip安装NumPy

python-3.x - 使用 pandas 进行多类分类的总体准确性