【发布时间】:2020-11-08 11:37:14
【问题描述】:
我需要使用 url 下载图像。我设法获得了我需要下载的图像的网址,但现在我不知道如何将它下载到我的本地计算机上。我正在使用谷歌 colab/jupyter。谢谢! 到目前为止,这是我的代码:
from bs4 import BeautifulSoup
import requests
import json
import urllib.request
#use Globe API to get data
#input userid - plan: have program read userids from csv or excel file
userid = xxxxxxxx
#use Globe API to get data
source = requests.get('https://api.globe.gov/search/v1/measurement/protocol/measureddate/userid/?protocols=land_covers&startdate=2020-05-04&enddate=2020-07-16&userid=' + str(userid) +'&geojson=FALSE&sample=FALSE').text
#set up BeautifulSoup4
soup = BeautifulSoup(source, 'lxml')
#Isolate the Json data and put it into a string called "paragraph"
body = soup.find('body')
paragraph = body.p.text
#load the string into a python object
data = json.loads(paragraph)
#pick out the needed information and store them
for landcover in data['results']:
siteId = landcover['siteId']
measuredDate = landcover['measuredDate']
latitude = landcover['latitude']
longitude = landcover['longitude']
protocol = landcover['protocol']
DownURL = landcover['data']['landcoversDownwardPhotoUrl']
#Here is where I want to download the url contained in 'DownURL'
【问题讨论】:
-
感谢您的链接。不幸的是,我已经尝试过使用该功能,但它不起作用,因为我想使用它的 url 下载图像,这与下载在 colab 中创建的文件不同。我还是一个初学者,所以我可能会误解一些东西
-
是的,您能够做到这一点的唯一方法是将文件保存在 colab 中,然后使用该函数。否则我只会在本地运行代码,而不是在 colab 中直接下载。
-
感谢您的建议!在 colab 中保存图像会是什么样子?有哪些功能?
-
应该是类似 imwrite 的东西,但我会四处寻找一个功能来保存适合你的图像
标签: python-3.x image download jupyter-notebook google-colaboratory