数据测试 如何解析 xml 中的数据进行数据库的进行比对

Sky.Tang · 2017年12月28日 · 最后由 songyudongqiang 回复于 2019年01月28日 · 1340 次阅读

最近遇到一些接入第三方的数据需要验证数据库的准确性,刚好学了一段时间 python,进简单的应用下。

目的:解析 xml 中的数据和数据库进行比对, 我这边暂时把 xml 中的数据解析出来保存到 excel,在通过对比工具和数据库里的数据做比对。
下面直接上代码:

# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import requests
import xlwt

proxies = {
    "http": "10.19.110.55:8080",
}

url = "xxxxxx"  # url中的数据未xml格式

page = requests.get(url, proxies=proxies)  # 读取xml接口中的数据
t = page.text

soup = BeautifulSoup(t, 'xml')  # 实例化soup
str1 = ''
with open(r'D:\pycharm\test1\qiu.txt', 'wb') as fp:  # 打开本地空文档存放解析的xml数据
    for match in soup.find_all('match'):  # 查找match标签包含的所有内容,生成一个列表
        matchInfo = match.matchInfo
        description = matchInfo.description
        str1 += description.text + '    ' + matchInfo['id'] + '\r\n'
    fp.write(str1.encode())

# 保存到excel
wb = xlwt.Workbook()  # 创建Excel
ws = wb.add_sheet('shuJu')
f = open(r'D:\pycharm\test1\qiu.txt', encoding='utf-8')

for j in range(0, 380):  # 解析出来的数据一共380行
    x = f.readline()  # 一行一行读取
    y = x.split('    ')  # 4个空格打断
    for i in range(0, 2):
        ws.write(j, i, y[i])
wb.save('shuJu.xls')

下面的图为接口中的 xml 数据

第一次发布,多多指教

共收到 1 条回复 时间 点赞

你用的什么比对工具啊,求告知,谢谢

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册