doufei2194
doufei2194
2016-11-10 15:58

如何在python脚本中使用已定义的变量自定义PHP url标记?

  • python
  • json
  • php

I am getting a TypeError: cannot concatenate 'str' and 'int' objects when i want to customise the url token where "id" is needed.

In place of one single result returned by a single id (say 303) i want to have the result for all the ids declared in the variable "station" retrieved from another url. the code is as follows:

import urllib2
import json

#assign the url
url="http://ewodr.wodr.poznan.pl/doradztwo/swd/swd_api.php?dane={%22token%22:%22pcss%22,%22operacja%22:%22stacje%22}"

# open the url 
json_obj= urllib2.urlopen(str(url))
output= json.load(json_obj) 
station_res= output ['data'] ['features']
for item in station_res:
    station= item['id']
url1="http://ewodr.wodr.poznan.pl/doradztwo/swd/meteo_api.php?dane={%22token%22:%22pcss%22,%22id%22:}" +str(station)
    json_obj2= urllib2.urlopen(str(url1))
    output2= json.load(json_obj2)
    for item2 in output2 ['data']:
        print item2

I tried to put "stations" as a string in the "url1" but it still doesn't recognise the url and returns an error:

Traceback (most recent call last):
    `File "stations.py", line 23, in <module>`
        `for item2 in output2 ['data']:`

KeyError: 'data'

</div>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答