python - 如何从 URL 下载多个图像
问题描述
我正在尝试使以下代码正常工作。我认为这很接近,但我不断收到一个错误,在最后一行显示“解析时意外 EOF”。
import requests
import subprocess
import os
import urllib.request
from bs4 import BeautifulSoup
os.chdir("C:/Users/Excel/Downloads/")
request = requests.get("http://ottofrello.dk/malerierstor.htm")
content = request.content
soup = BeautifulSoup(content, "html.parser")
element = soup.find_all("img")
for img in element:
urllib.request.urlretrieve("http://ottofrello.dk/" + img.get('src') + "," + img.get('src') + ")"
我正在使用 Python 3.6。
解决方案
您可能希望将最后一行重写为:
urllib.request.urlretrieve("http://ottofrello.dk/" + img.get('src'), img.get('src'))
当图像路径不仅是文件名,而是实际路径时,您仍然会偶尔出现错误,但您将朝着正确的方向前进,并且可以稍后解决这个问题。
推荐阅读
- bash - Bash 脚本,命令结束后更新输出
- javascript - If a PHP server script calls a javascript file with a secret key is it exposed?
- python - Averaging filter in image processing
- date - IBM DataStage, for Date Column, all rows failed to inserted after 0 replace it with a Default value
- c - C 中的泰勒级数(sin(240) 和 sin(300) 的问题)
- r - 使用 as.Date 和 as_date 将 `dttm` 转换为 `date` 格式会在 R 中给出不同的结果
- json - 尽管文件存在于上述目录中,为什么我没有收到此类文件或目录错误?
- java - 全局运行 Maven 插件
- dialogflow-es - 在 agentId 的平台响应中找不到 RichResponse 或 SystemIntent:
- vuetify.js - 使用 Vuetify 2 的父 v-data-table 中可扩展子组件的问题