简体   繁体   English

如何从URL下载多个图像

[英]How to download multiple images from a URL

I am trying to get the below code working. 我正在尝试使以下代码正常工作。 I think this is close, but I keep getting an error that reads 'unexpected EOF while parsing' on the last line. 我认为这已经很接近了,但是在最后一行上,我不断收到错误消息,显示“解析时出现意外的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') + ")"

I am using Python 3.6. 我正在使用Python 3.6。

you may want to rewrite the last line as: 您可能需要将最后一行重写为:

urllib.request.urlretrieve("http://ottofrello.dk/" + img.get('src'), img.get('src'))

still you'll get occasional errors when the image path is not just a file name, but an actual path, but you'll be going in the right direction, and can solve this later. 当图像路径不仅是文件名,而是实际路径时,仍然会偶尔出现错误,但是您的方向正确,以后可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM