接口自动化——xml响应断言

什么是 XML

  • 可扩展标记语言(Extensible Markup Language)的缩写
  • 也是一种结构化的数据

xml解析

import xml.etree.ElementTree as ET

root = ET.fromstring(countrydata)

root.findall(".")

root.findall("./country/neighbor")

root.findall(".//year/..[@name='Singapore']")

root.findall(".//*[@name='Singapore']/year")

root.findall(".//neighbor[2]")

XPath 断言

from requests_xml import XMLSession

session = XMLSession()

r = session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')

r.xml.links

item = r.xml.xpath('//item', first=True)

print(item.text)