【发布时间】:2017-04-06 13:51:04
【问题描述】:
我不明白为什么会发生这个错误。 首先,我写了
import urllib.request
from bs4 import BeautifulSoup
import time
import os
def download_image(url,name):
path = "./scrape_image/"
imagename = str(name) + ".jpg"
if not os.path.exists(path):
os.makedirs(path)
print(path)
urllib.request.urlretrieve(url,path+imagename)
url = "https://api.XXXkeyword=YYY&limit=1000"
response = urllib.request.urlopen(url)
rss = response.read().decode("utf-8")
soup = BeautifulSoup(rss, "xml")
name=0
for s in soup.find_all("photo"):
url = s.find_all("image_url")[0].string
name+=1
download_image(url, name)
通过运行这段代码,我可以从 API 中获取 1 张图片。但是原本正确的代码可以从 API 中获取 1000 张图片。我在第一个代码中修复了缩进,所以我的代码是这样的
import urllib.request
from bs4 import BeautifulSoup
import time
import os
def download_image(url,name):
path = "./image/"
imagename = str(name) + ".jpg"
if not os.path.exists(path):
os.makedirs(path)
print(path)
urllib.request.urlretrieve(url, path+imagename)
time.sleep(1)
url = "https://api.XXXkeyword=YYY&limit=1000"
response = urllib.request.urlopen(url)
rss = response.read().decode("utf-8")
soup = BeautifulSoup(rss, "xml")
name = 0
for s in soup.find_all("photo"):
url = s.find_all("image_url")[0].string
name+=1
download_image(url,name)
最后,我可以从 API 中获取 1000 张图片。但是我不明白为什么我可以通过修复缩进来做到这一点。请给我一些解释。
【问题讨论】:
-
因为它是 Python...?
标签: python-3.x tensorflow