BeautifulSOUP和OpenStreetMap XML中的嵌套标签和属性



请帮助为任务编写有意义的代码:我需要计算所有标签";"方式";在XML OpenStreet Map文件中;nd";标签,并输入标签"way"的id,其中包括最大数量的标签";nd";。如果有几个ide,那么按字母顺序输入第一个。看起来很简单,但我不懂怎么操作。(我只认为使用词汇会有用(这是代码:

from urllib.request import urlopen, urlretrieve
from bs4 import BeautifulSoup

resp = urlopen('https://stepik.org/media/attachments/lesson/245681/map2.osm') # 
xml = resp.read().decode('utf8') # 
soup = BeautifulSoup(xml, 'xml') # делаем суп с помощью lxml
cnt = 0
names ={}
for way in soup.find_all('way'): # go through the nodes
flag=False
for nd in way('nd'):
flag=True
if nd['k'] == 'id':
name=nd['v']
if flag:
if name not in names:
names[name]=0
names[name]+=1
print(sort(names)) 

您可以使用max()内置方法来查找<nd>数量最大的<way>标记。

例如:

import requests
from bs4 import BeautifulSoup

url = 'https://stepik.org/media/attachments/lesson/245681/map2.osm'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
num_way = len(soup.select('way'))
w = max(sorted(soup.select('way:has(nd)'), reverse=True, key=lambda tag: int(tag['id'])), key=lambda tag: len(tag.select('nd')))
print('number of <way>:', num_way)
print('id:', w['id'])
print('quantity of <nd>:', len(w.select('nd')))

打印:

number of <way>: 3181
id: 227140108
quantity of <nd>: 249

最新更新