admin 管理员组

文章数量: 887021


2023年12月21日发(作者:redis父子进程)

基于Python的全国邮政编码查询api调用代码实例

代码描述:基于Python的全国邮政编码查询api调用代码实例

代码平台:聚合数据

#!/usr/bin/python

# -*- coding: utf-8 -*-

import

json, urllib

from

urllib import

urlencode

#----------------------------------

# 邮编查询调用示例代码 - 聚合数据

# 在线接口文档:/docs/66

#----------------------------------

def

main():

#配置您申请的APPKey

appkey =

"*********************"

#1.邮编查询地名

request1(appkey,"GET")

#2.省份城市区域列表

request2(appkey,"GET")

#3.地名查询邮编

request3(appkey,"GET")

#邮编查询地名

def

request1(appkey, m="GET"):

url =

"/postcode/query"

params =

{

"postcode"

: "", #邮编,如:215001

"key"

: appkey, #应用APPKEY(应用详细页查询)

"page"

: "", #页数,默认1

"pagesize"

: "", #每页返回,默认:20,最大不超过50

"dtype"

: "", #返回数据的格式,xml或json,默认json

}

params =

urlencode(params)

if

m =="GET":

f =

n("%s?%s"

%

(url, params))

else:

f =

n(url, params)

n"])

content =

()

res =

(content)

if

res:

error_code =

res["error_code"]

if

error_code ==

0:

#成功请求

print

res["result"]

else:

print

"%s:%s"

%

(res["error_code"],res["reaso else:

print

"request api error"

#省份城市区域列表

def

request2(appkey, m="GET"):

url =

"/postcode/pcd"

params =

{

"key"

: appkey, #应用APPKEY(应用详细页查询)

"dtype"

: "", #返回数据的格式,xml或json,默认json

}

params =

urlencode(params)

if

m =="GET":

f =

n("%s?%s"

%

(url, params))

else:

f =

n(url, params)

content =

()

res =

(content)

if

res:

error_code =

res["error_code"]

if

error_code ==

0:

#成功请求

print

res["result"]

else:

print

"%s:%s"

%

(res["error_code"],res["reaso

n"])

else:

print

"request api error"

#地名查询邮编

def

request3(appkey, m="GET"):

url =

"/postcode/search"

params =

{

"pid"

: "", #省份ID

"cid"

: "", #城市ID

"did"

: "", #区域ID

"q"

: "", #地名关键字,如:木渎

"key"

: appkey, #应用APPKEY(应用详细页查询)

"dtype"

: "", #返回数据的格式,xml或json,默认json

}

params =

urlencode(params)

if

m =="GET":

f =

n("%s?%s"

%

(url, params))

else:

f =

n(url, params)

content =

()

res =

(content)

if

res:

error_code =

res["error_code"]

if

error_code ==

0:

#成功请求

print

res["result"]

else:

print

"%s:%s"

%

(res["error_code"],res["reaso

n"])

else:

print

"request api error"

if

__name__ ==

'__main__':

main()


本文标签: 查询 代码 数据 调用 返回