从列表中打开 URL 并写入数据

1 投票
1 回答
1220 浏览
提问于 2025-04-18 12:21

我正在写一段代码,这段代码会创建几个网址,然后把这些网址存储在一个列表里。接下来的步骤是,打开每个网址,下载数据(这些数据只是文本,格式是XML或JSON),然后保存下载的数据。

我的代码运行得很好,多亏了这里的在线社区。但在打开网址和下载数据这一步卡住了。我想让url.request循环遍历我创建的网址列表,逐个打开每个网址,显示内容,然后继续下一个网址。但是它只循环创建网址,之后就没反应了。没有任何反馈,什么都没有。

import urllib.request

.... some calculations for llong and llat ....


#create the URLs and store in list
urls = []
for lat,long,lat1,long1 in (zip(llat, llong,llat[1:],llong[1:])):
    for pages in range (1,17):
        print ("https://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=5.b&nojsoncallback=1&page={}&per_page=250&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(pages,long,lat,long1,lat1))
print (urls)


#accessing the website 
data = []
for amounts in urls:
    response = urllib.request.urlopen(urls)
    flickrapi = data.read()
    data.append(+flickrapi)
    data.close()
    print (data)

我哪里出错了呢?

接下来的步骤是,下载数据并把它们保存到文件里或者其他地方,以便后续处理。因为我会收到大量的数据,真的是很多很多,所以我不太确定用什么方式存储这些数据,以便用R(或者Python?- 需要对数据进行一些统计工作)处理。有什么建议吗?

1 个回答

0

你没有把生成的链接加到链接列表里,而是直接把它们打印出来了:

print ("https://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=5.b&nojsoncallback=1&page={}&per_page=250&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(pages,long,lat,long1,lat1))

应该是:

urls.append("https://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&api_key=5.b&nojsoncallback=1&page={}&per_page=250&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(pages,long,lat,long1,lat1))

这样你就可以像计划的那样遍历这些链接了。

但是在下面这一行你会遇到错误:

response = urllib.request.urlopen(urls)

在这里,你把所有的链接都传给了 urlopen,其实你应该只传一个链接,从你叫做 amounts 的链接列表中取出一个链接,像这样:

response = urllib.request.urlopen(amounts)

撰写回答