json - How to apply a variable in the url token which is of integer type using python script? -
i getting typeerror: cannot concatenate 'str' , 'int' objects
when wanted customise url token "id" needed.
in place of 1 single result returned single id (say 303) want have result ids declared in variable "station" retrieved url. code follows:
import urllib2 import json #assign url url="http://ewodr.wodr.poznan.pl/doradztwo/swd/swd_api.php?dane={%22token%22:%22pcss%22,%22operacja%22:%22stacje%22}" # open url json_obj= urllib2.urlopen(str(url)) output= json.load(json_obj) station_res= output ['data'] ['features'] 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
now tried put "stations" string in "url1" still doesn't recognise url , returns error:
traceback (most recent call last): `file "stations.py", line 23, in <module>` `for item2 in output2 ['data']:`
keyerror: 'data'
the problem add id
after '}' url looks
http://ewodr.wodr.poznan.pl/doradztwo/swd/meteo_api.php?dane={%22token%22:%22pcss%22,%22id%22:}331
when should
http://ewodr.wodr.poznan.pl/doradztwo/swd/meteo_api.php?dane={%22token%22:%22pcss%22,%22id%22:331}
so suggest :
url1 = 'http://ewodr.wodr.poznan.pl/doradztwo/swd/meteo_api.php?dane={%22token%22:%22pcss%22,%22id%22:' + str(station) + '}'
and don't need write str(url1)
because string already.
Comments
Post a Comment