[Tutor] encoding question
Alex Kleider
akleider at sonic.net
Sun Jan 5 05:16:10 CET 2014
On 2014-01-04 18:44, Danny Yoo wrote:
> Hi Alex,
>
>
> According to:
>
> http://www.hostip.info/use.html
>
> there is a JSON-based interface. I'd recommend using that one! JSON
> is a format that's easy for machines to decode. The format you're
> parsing is primarily for humans, and who knows if that will change in
> the future to make it easier to read?
>
> Not only is JSON probably more reliable to parse, but the code itself
> should be fairly straightforward. For example:
>
> #########################################################
> ## In Python 2.7
> ##
>>>> import json
>>>> import urllib
>>>> response = urllib.urlopen('http://api.hostip.info/get_json.php')
>>>> info = json.load(response)
>>>> info
> {u'country_name': u'UNITED STATES', u'city': u'Mountain View, CA',
> u'country_code': u'US', u'ip': u'216.239.45.81'}
> #########################################################
>
>
This strikes me as being the most elegant solution to date, and I thank
you for it!
The problem is that the city name doesn't come in:
alex at x301:~/Python/Parse$ cat tutor.py
#!/usr/bin/env python
# -*- coding : utf -8 -*-
# file: 'tutor.py'
"""
Put your docstring here.
"""
print "Running 'tutor.py'......."
import json
import urllib
response = urllib.urlopen\
('http://api.hostip.info/get_json.php?ip=201.234.178.62&position=true')
info = json.load(response)
print info
alex at x301:~/Python/Parse$ ./tutor.py
Running 'tutor.py'.......
{u'city': None, u'ip': u'201.234.178.62', u'lat': u'10.4',
u'country_code': u'CO', u'country_name': u'COLOMBIA', u'lng':
u'-75.2833'}
If I use my own IP the city comes in fine so there must still be some
problem with the encoding.
should I be using
encoding = response.headers.getparam('charset')
in there somewhere?
Any ideas?
More information about the Tutor
mailing list