如何調(diào)用API接口,查詢手機(jī)號(hào)碼歸屬地

本篇內(nèi)容介紹了“如何調(diào)用API接口,查詢手機(jī)號(hào)碼歸屬地”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司成立于2013年,先為修文等服務(wù)建站,修文等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為修文企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

以聚合數(shù)據(jù)平臺(tái)的接口為例,講解下如何從MySQL數(shù)據(jù)庫(kù)獲取電話號(hào)碼,查詢歸屬地并插入到數(shù)據(jù)庫(kù)。

代碼示例如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib, sys, pymysql
from urllib import urlencode, urlopen
reload(sys)
sys.setdefaultencoding('utf8')
 
#調(diào)用接口,獲取結(jié)果為二維字典
def getPageCode(url, params):
    params = urlencode(params)
    f = urllib.urlopen(url, params)
    content = f.read()
    res = json.loads(content)
    return res
    #print('Error code: %s'%res["resultcode"])
 
#對(duì)api接口返回?cái)?shù)據(jù)處理。
def resTest(resdata):   
    if resdata["resultcode"] == "200":
        #返回有效結(jié)果
        return resdata["result"] 
    else:
        #定義錯(cuò)誤字典,resdata["resultcode"]為接口返回的Error code
        Errorinfo = {'province':'Error code', 'city':resdata["resultcode"]}
        return Errorinfo
        
#處理字典為單元組列表 
def dictDate(data):
    province = data["province"]
    city = data["city"]
    res.append((phoneNum, province, city))
    #將省和市處理為一個(gè)元素:遼寧,葫蘆島
    res1,res2,res3 = res[0][0],res[0][1],res[0][2]
    symbol = ','
    resz = [(res1,res2+symbol+res3)]
    #print('list res is : %s'%resz)
    return resz
 
    
#連接數(shù)據(jù)庫(kù)
dblink = pymysql.connect(
    host = "10.10.10.31",
    user = "abc",
    password = "123456",
    database = "test",
    charset = "utf8")
 
#查詢數(shù)據(jù)
def select(db):
    cursor = db.cursor()
    cursor.execute("select phoneNum from test.userinfo")
    #cursor.execute("select phoneNum from test.userinfo order by id;")
    # 使用 fetchone() 方法獲取單條數(shù)據(jù).fetchall()獲取所有行
    data = cursor.fetchall()
    #print data
    return data
 
#插入數(shù)據(jù)
def install(db, data):
    
    cursor = db.cursor()
    sql = "update `test`.`userinfo` set location=%s where phoneNum = %s"
    #data=(第一個(gè)%s,第二個(gè)%s)
    data = (data[0][1], data[0][0])
    cursor.execute(sql, data)
    db.commit()
 
if __name__ == "__main__":
    url = "http://apis.juhe.cn/mobile/get"
    for phoneNum in select(dblink):
        phoneNum = phoneNum[0]
        params = {
            "phone": phoneNum,
            "key": "0ea8e44e4612fb794c29asdfde48hg",
            "dtype" : "json",
        }   
        res = []
        #調(diào)用getPageCode接口,resTest處理數(shù)據(jù)
        resdate = resTest((getPageCode(url, params)))
        #通過(guò)函數(shù)dictDate處理為單元組列表,[('12676512732', u'\u6d52\u6c5f,\u6e19\u5dde')]
        resUlt = dictDate(resdate)
        install(dblink, resUlt)
    dblink.close()

“如何調(diào)用API接口,查詢手機(jī)號(hào)碼歸屬地”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

分享文章:如何調(diào)用API接口,查詢手機(jī)號(hào)碼歸屬地
網(wǎng)站地址:http://muchs.cn/article0/isjjoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、建站公司響應(yīng)式網(wǎng)站、小程序開發(fā)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)