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

nested tags and attributes in BeautifulSOUP and OpenStreetMap XML

请帮助编写有意义的任务代码: 我需要计算 XML OpenStreet Map 文件中的所有标签“way”,每个标签中“nd”标签的数量,并输入标签的 ID 'way',其中包括最大数量的标签“和”。如果有多个 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() 内置方法查找 <way> 数量最多的标签 <nd>

例如:

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