问题描述
我尝试使用 python 从 XML 中的标签“CRS”中获取信息并收集所有 CRS 标签,或者我稍后将编辑代码以检查特定的 CRS。
网址:https://wms.geonorge.no/skwms1/wms.adm_enheter_historisk?service=WMS&request=GetCapabilities
这是我目前的代码:
import urllib.request,urllib.parse,urllib.error
import xml.etree.ElementTree as ET
url = 'https://wms.geonorge.no/skwms1/wms.adm_enheter_historisk?service=WMS&request=GetCapabilities'
uh = urllib.request.urlopen(url)
data = uh.read()
tree = ET.fromstring(data)
从这里我不知道如何处理 tree.find() 或 tree.findall()
谢谢。
解决方法
所以,这就是我所做的。我需要检查 XML 是否包含 CRS EPSG:3857。因此,我没有获取 CRS 标签中的所有信息,而是通过测试解析的 XML 是否包含文本“EPSG:3857”来解决这个问题。
import urllib.request,urllib.parse,urllib.error
import xml.etree.ElementTree as ET
url = 'https://wms.geonorge.no/skwms1/wms.adm_enheter_historisk?service=WMS&request=GetCapabilities'
uh = urllib.request.urlopen(url)
data = uh.read()
projection = "EPSG:3857"
if projection in str(data):
print("Contains")
else:
print("Contains not")
现在在另一个程序中实现以搜索多个 XML 文件。
,试试这个。
from simplified_scrapy import req,SimplifiedDoc
xml = req.get(
'https://wms.geonorge.no/skwms1/wms.adm_enheter_historisk?service=WMS&request=GetCapabilities'
)
doc = SimplifiedDoc(xml)
listCRS = doc.selects('CRS')
print(listCRS)