0

I'm trying to get IP location and other stuff from ipinfodb.com, but I'm stuck.

I want to split all of the values into new strings that I can format how I want later. What I wrote so far is:

resp = urllib2.urlopen('http://api.ipinfodb.com/v3/ip-city/?key=mykey&ip=someip').read()
    out = resp.replace(";", " ")
    print out

Before I replaced the string into new one the output was:

OK;;someip;somecountry;somecountrycode;somecity;somecity;-;42.1975;23.3342;+05:00

So I made it show only

OK someip somecountry somecountrycode somecity somecity - 42.1975;23.3342 +05:00

But the problem is that this is pretty stupid, because I want to use them not in one string, but in more, because what I do now is print out and it outputs this, I want to change it like print country, print city and it outputs the country,city etc. I tried checking in their site, there's some class for that but it's for different api version so I can't use it (v2, mine is v3). Does anyone have an idea how to do that?

PS. Sorry if the answer is obvious or I'm mistaken, I'm new with Python :s

1 Answer 1

1

You need to split the resp text by ;:

out = resp.split(';')

Now out is a list of values instead, use indexes to access various items:

print 'Country: {}'.format(out[3])

Alternatively, add format=json to your query string and receive a JSON response from that API:

import json

resp = urllib2.urlopen('http://api.ipinfodb.com/v3/ip-city/?format=json&key=mykey&ip=someip')
data = json.load(resp)

print data['countryName']
Sign up to request clarification or add additional context in comments.

2 Comments

Oh yeah I did resp.split by ;, but never knew I can format them lol. Thanks! Edit: Yes that's the lib I was talking about, it's for api version 2.
@maikati: ah, removed the library link then. It's really just a thin wrapper in any case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.