下面这段代码是验证接口返回的数据是否与数据表的数据一致,并且把不一致的保存。
# -*- coding: utf-8 -*-
import urllib,urllib.request
import requests,xlrd,json
no=open('D:\\pythonfile\\no.txt','r+') #将不符合程序的url和poi写入此文件
with open('D:\\pythonfile\\poi.txt','r', encoding='UTF-8') as f:#打开数据文件
for poi in f.readlines():
list_poi=poi.split('","')
poid= list_poi[8].encode('gbk')
id=list_poi[1].strip('""')#取出数据文件中的poi的id
#print id
url='http://10.10.40.124:8807/search/detail?id='+id
# print url
html=requests.get(url)
s=json.loads(html.content)
name=s['pois'][0]['name']#取出url返回结果中的第一个poi的名称
# print name
try:
if poid == name:
print pass
else:
print false
name1 = name.encode('gbk').strip('""')
poid1 = poid.encode('gbk').strip('""')
no.write(url + '\n' + poid1 + '\n' + name1 + '\n')
except:
pass