从mysql数据库获取电话号码,查询归属地并插入到数据库

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib, sys, pymysql
from urllib import urlencode, urlopen
reload(sys)
sys.setdefaultencoding('utf8') #调用接口,获取结果为二维字典
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"]) #对api接口返回数据处理。
def resTest(resdata):
if resdata["resultcode"] == "200":
#返回有效结果
return resdata["result"]
else:
#定义错误字典,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))
#将省和市处理为一个元素:辽宁,葫芦岛
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 #连接数据库
dblink = pymysql.connect(
host = "10.10.10.31",
user = "abc",
password = "123456",
database = "test",
charset = "utf8") #查询数据
def select(db):
cursor = db.cursor()
cursor.execute("select phoneNum from test.userinfo")
#cursor.execute("select phoneNum from test.userinfo order by id;")
# 使用 fetchone() 方法获取单条数据.fetchall()获取所有行
data = cursor.fetchall()
#print data
return data #插入数据
def install(db, data): cursor = db.cursor()
sql = "update `test`.`userinfo` set location=%s where phoneNum = %s"
#data=(第一个%s,第二个%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 = []
#调用getPageCode接口,resTest处理数据
resdate = resTest((getPageCode(url, params)))
#通过函数dictDate处理为单元组列表,[('12676512732', u'\u6d52\u6c5f,\u6e19\u5dde')]
resUlt = dictDate(resdate)
install(dblink, resUlt)
dblink.close()

调用API接口,查询手机号码归属地(1)https://www.cnblogs.com/outsrkem/p/11219743.html

调用API接口,查询手机号码归属地(2)https://www.cnblogs.com/outsrkem/p/11221003.html

调用API接口,查询手机号码归属地(3)https://www.cnblogs.com/outsrkem/p/11221604.html

调用phone库,查询手机号码归属地(4)https://www.cnblogs.com/outsrkem/p/11236509.html

调用API接口,查询手机号码归属地(3)的更多相关文章

  1. 调用API接口,查询手机号码归属地(1)

    使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...

  2. 调用API接口,查询手机号码归属地(2)

    使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...

  3. C#使用windows服务定时调用api接口

    使用VS创建windows服务项目: 创建好项目  会出现一个设计界面 右键弹出对话框 选择添加安装程序 名字什么的自己可以改: 项目目录: 打开项目中的ProjectInstaller.Design ...

  4. Python调用API接口的几种方式 数据库 脚本

    Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...

  5. Python调用API接口的几种方式

    Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...

  6. jquery使用淘宝接口跨域查询手机号码归属地实例

    <h1>手机号码归属地查询</h1>    <div class="outer">        <p>请输入手机号码</p& ...

  7. 调用webservice查询手机号码归属地信息

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...

  8. Apsara Clouder专项技能认证:实现调用API接口

    一.API 简介 1.API 的概念 API(Application Programming Interface应用程序编程接口)是一些预定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访 ...

  9. spring boot 通过feign调用api接口

    目的:远程调用服务器api,直接上步骤: 1,添加maven依赖,这是必须的: <dependency> <groupId>org.springframework.cloud& ...

随机推荐

  1. [转载]真正的inotify+rsync实时同步 彻底告别同步慢

    原文链接http://www.ttlsa.com/web/let-infotify-rsync-fast/ 背景我们公司在用inotify+rsync做实时同步,来解决分布式集群文件一致性的问题.但当 ...

  2. jQuery.inArray()方法

    $.inArray() 函数用于在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1) 提示:源数组不会受到影响,过滤结果只反映在返回的结果数组中. 语法 $.inArray( value, ...

  3. PAT甲级——A1153 DecodeRegistrationCardofPAT【25】

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  4. 数据结构与算法简记--Trie树

    Trie树 概念 多叉树,节点为字符串中的单个字符. Trie 树的本质,就是利用字符串之间的公共前缀,将重复的前缀合并在一起. 将多个字符串按字符拆分插入Trie树,用于字符串查找,关键词提示等 举 ...

  5. 20-python基础-python3-reversed()函数

    描述 reversed 函数返回一个反转的迭代器. 语法 reversed(seq) 参数 seq -- 要转换的序列,可以是 tuple, string, list 或 range. # 字符串 s ...

  6. 写第一个 swift,iOS 8.0程序

    class ViewController: UIViewController { @IBAction func btn_click(sender : AnyObject) { println(&quo ...

  7. ionic3 emoj表情包插件 emoji-picker

    1.效果演示: 2.安装扩展包依赖 npm i @ionic-tools/emoji-picker --save 3.app.module.ts中导入插件 import { EmojiPickerMo ...

  8. mysql添加外键语句

    sql语句格式: · 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key (从表外键字段) references 主表(主 ...

  9. strings - 显示文件中的可打印字符

    总览 (SYNOPSIS) strings [-a|-|--all] [-f|--print-file-name] [-o] [--help] [-v|--version] [-n min-len|- ...

  10. termcap - 终端功能数据库

    描述 DESCRIPTION termcap 数据库是一个过时 (obsolete) 工具,用来描述以字符为单位的终端和打印机的功能.它之所以被保留,是为了兼容古老的程序:新程序应当使用 termin ...