1

我想在地球地图中可视化来自用户“airqualityegg”的所有提要。为了做到这一点,我用 Python 编写了以下脚本(如果您要自己尝试,请在您使用的文本编辑器中正确缩进代码):

import json  
import urllib
import csv

list=[]

for page in range(7):
   url = 'https://api.xively.com/v2/feeds?user=airqualityegg&per_page=100page='+str(page)
   rawData=urllib.urlopen(url)

   #Loads the data in json format
   dataJson = json.load(rawData)
   print dataJson['totalResults']
   print dataJson['itemsPerPage']

   for entry in dataJson['results']:

      try:
          list2=[]
          list2.append(entry['id'])
          list2.append(entry['creator'])
          list2.append(entry['status'])
          list2.append(entry['location']['lat'])
          list2.append(entry['location']['lon'])
          list2.append(entry['created'])
          list.append(list2)

      except:

          print 'failed to scrape a row'

def escribir():
   abrir = open('all_users2_andy.csv', 'w')
   wr = csv.writer(abrir, quoting=csv.QUOTE_ALL)
   headers = ['id','creator', 'status','lat', 'lon', 'created']
   wr.writerow (headers)

   for item in list:
       row=[item[0], item[1], item[2], item[3], item[4], item[5]]
       wr.writerow(row)
       abrir.close()

escribir()

我已经调用了 7 个页面,因为该用户发布的提要总数为 684(正如您直接在浏览器中编写时所看到的那样“ https://api.xively.com/v2/feeds?user=airqualityegg ” )

运行此脚本生成的 csv 文件确实显示了重复的行,这可以解释为每次调用页面时结果的顺序都会发生变化。因此,同一行可以包含在不同调用的结果中。出于这个原因,我得到的结果不那么独特。

您知道为什么不同页面中包含的结果可能不是唯一的吗?

谢谢,玛丽亚

4

1 回答 1

0

您可以尝试通过 order=created_at(请参阅文档)。

问题是因为默认情况下order=updated_at,因此每个页面上的结果可能会有所不同。

您还应该考虑使用官方 Python 库。

于 2013-07-13T15:22:56.783 回答