Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列表中打开URL并写入数据_Python_Url_Screen Scraping_Bigdata_Urllib - Fatal编程技术网

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

Python 从列表中打开URL并写入数据,python,url,screen-scraping,bigdata,urllib,Python,Url,Screen Scraping,Bigdata,Urllib,我正在编写一个代码,创建几个URL,这些URL再次存储在一个列表中。 下一步是,打开每个URL,下载数据(仅为文本,格式为XML或JSON)并保存下载的数据 多亏了这里的在线社区,我的代码工作得很好。它在打开URL并下载数据时卡住了。我希望url.request使用我创建的url在列表中循环,分别调用每个url,打开它,显示它,然后转到下一个url。但它只执行创建URL的循环,然后什么也不做。没有反馈,什么都没有 import urllib.request .... some calculat

我正在编写一个代码,创建几个URL,这些URL再次存储在一个列表中。 下一步是,打开每个URL,下载数据(仅为文本,格式为XML或JSON)并保存下载的数据

多亏了这里的在线社区,我的代码工作得很好。它在打开URL并下载数据时卡住了。我希望url.request使用我创建的url在列表中循环,分别调用每个url,打开它,显示它,然后转到下一个url。但它只执行创建URL的循环,然后什么也不做。没有反馈,什么都没有

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?——需要做一些统计工作)处理数据的最佳存储方式是什么。有什么建议吗?

您没有将生成的url附加到url列表,而是打印它们:

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))
然后,您可以按计划迭代URL

但您将在以下行中遇到错误:

response = urllib.request.urlopen(urls)
在这里,您将整个url集输入到
urlopen
,在这里您应该从您命名为
的url中传入一个url,如下所示:

response = urllib.request.urlopen(amounts)

这样做,我得到的反馈是
Traceback(最后一次调用):文件“/Users/christoph/Desktop/test.py”,第54行,响应=urllib.request.urlopen(url)文件“/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py”,第156行,在urlopen return opener.opener(url,data,timeout)文件中“/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py”,第460行,在open req.timeout=timeout AttributeError中:“list”对象没有属性“timeout”
ah,太棒了!下一步是下载数据。谢谢!