SamB SamB - 27 days ago 10
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
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'

Answer

Try with:

station= item ['id']
url1="http://ewodr.wodr.poznan.pl/doradztwo/swd/meteo_api.php?dane={%22token%22:%22pcss%22,%22id%22:}" +str(station)

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