aircraft
aircraft

Reputation: 26924

Get HTTPConnectionPool data

I use a open API get the bellow information using its access key:

HTTPConnectionPool(host='freeipapi.17mon.cn', port=80): Max retries exceeded with url: /183.11.178.118 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x103780780>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
{'status': 0, 'address': 'CN|广东|深圳|None|CHINANET|0|0', 'content': {'address_detail': {'province': '广东省', 'city_code': 340, 'street': '', 'city': '深圳市', 'district': '', 'street_number': ''}, 'address': '广东省深圳市', 'point': {'x': '12693451.44', 'y': '2560682.35'}}}

You see, the later data {'status': 0, 'address': 'CN|广东|深圳|None|CHINANET|0|0', 'content': {'address_detail': {'province': '广东省', 'city_code': 340, 'street': '', 'city': '深圳市', 'district': '', 'street_number': ''}, 'address': '广东省深圳市', 'point': {'x': '12693451.44', 'y': '2560682.35'}}} is my need.

How can I get it?

The code is bellow:

import urllib, urllib.request
import json

ak_url="http://api.map.baidu.com/location/ip"

result = requests.get(ak_url,{"ip":"183.11.178.1","ak":BAIDU_IPADDR_AK})

print(result.json())  # There prints the upper data.

Upvotes: 1

Views: 278

Answers (2)

Karim N Gorjux
Karim N Gorjux

Reputation: 3033

Use requests. Much easier. pip install requests

Then:

import requests

ak_url="http://api.map.baidu.com/location/ip"
BAIDU_IPADDR_AK = 'test'
result = requests.get(ak_url,{"ip":"183.11.178.1","ak":BAIDU_IPADDR_AK})
print(result.json())

I get this result:

{'status': 200, 'message': 'APP不存在,AK有误请检查再重试'}

Upvotes: 0

Adelin
Adelin

Reputation: 8219

Your data is actually returned by result.json().

Perhaps you are confused by the extra HTTPConnectionPool details.

That could be an indicator that result.json() has a custom representation (as in, it implements __repr__ or __str__) and appends that data behind the scenes when printing.

Upvotes: 2

Related Questions