调用API接口,查询手机号码归属地(3)
从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)的更多相关文章
- 调用API接口,查询手机号码归属地(1)
使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...
- 调用API接口,查询手机号码归属地(2)
使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...
- C#使用windows服务定时调用api接口
使用VS创建windows服务项目: 创建好项目 会出现一个设计界面 右键弹出对话框 选择添加安装程序 名字什么的自己可以改: 项目目录: 打开项目中的ProjectInstaller.Design ...
- Python调用API接口的几种方式 数据库 脚本
Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...
- Python调用API接口的几种方式
Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...
- jquery使用淘宝接口跨域查询手机号码归属地实例
<h1>手机号码归属地查询</h1> <div class="outer"> <p>请输入手机号码</p& ...
- 调用webservice查询手机号码归属地信息
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...
- Apsara Clouder专项技能认证:实现调用API接口
一.API 简介 1.API 的概念 API(Application Programming Interface应用程序编程接口)是一些预定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访 ...
- spring boot 通过feign调用api接口
目的:远程调用服务器api,直接上步骤: 1,添加maven依赖,这是必须的: <dependency> <groupId>org.springframework.cloud& ...
随机推荐
- -webkit-box 高度自动填满
<style> .box{ display: -webkit-box; -webkit-box-orient: vertical; height: 200px; background: # ...
- activiti7删除流程定义的相关信息
package com.zcc.activiti02; import org.activiti.engine.ProcessEngine;import org.activiti.engine.Proc ...
- VC的小工具查询exe的依赖
查看程序或动态库所依赖的动态库 dumpbin /dependents abc.exe 查看动态库的输出函数 dumpbin /exports abc.dll
- Springboot的Mybatis逆向工程
1.pom.xml添加mybatis和逆向插件依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> ...
- elementUI 限制上传个数limit
:limit='1' // 个数限制.
- JS window对象 screen对象 screen对象用于获取用户的屏幕信息。 语法: window.screen.属性
screen对象 screen对象用于获取用户的屏幕信息. 语法: window.screen.属性 对象属性:
- AtCoder Beginner Contest 127 D,E,F
D Integer Cards 题意:先给出n个数字,然后可以有m次操作,每次操作以数字对(x,y)表示最多能选x个数字把它变成y,问经历m次操作后n个数字和最大为多少? 解法:一个明显正确的做法是: ...
- 初撩Django-RESTful-rest_framework序列化(将模型序列化为JSON)
官方网站: https://www.django-rest-framework.org/ 翻译网站:https://q1mi.github.io/Django-REST-framework-docum ...
- github fork代码后提交
点击他人github上的fork 在自己的Github上将代码拷贝下来 git clone 在本地修改代码后创建分支 git checkout -b work master(work为新建的特性分支, ...
- vue 表格 多选 换页保存前一页的状态
表格多选 点击下一页,上一页的状态没消失. 在表格 添加 row-key="id" 在表格行里添加 reserve-selection