SamB SamB - 1 year ago 148
PHP Question

How to customize a PHP url token with a defined variable in a python script?

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

# 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="{%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 "", line 23, in <module>`
`for item2 in output2 ['data']:`

KeyError: 'data'

Answer Source

Try with:

station= item ['id']
url1="{%22token%22:%22pcss%22,%22id%22:}" +str(station)

You are trying to join an String with an Integer, what generate this error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download