BeautifulSoup parse XML пространства имен - PullRequest
0 голосов
/ 27 марта 2020

У меня есть этот скрипт:

infile = open("t.gpx", "r")
contents = infile.read()
soup = BeautifulSoup(contents, 'xml')

wpts = soup.find_all('wpt')
names = soup.find_all('name')
texts = soup.find_all('text')
types = soup.find_all('type')
gnames = soup.find_all('groundspeak:name')
gplaced = soup.find_all('groundspeak:placed_by')
print()
for i in range(0, len(wpts)):
    print(names[i].get_text(), end=' ')
    print(texts[i].get_text(), end=' ')
    print(types[i].get_text(), end=' ')
    print(gnames[i].get_text(), end=' ')
    print(gplaced[i].get_text(), end='\n')

и входной файл t.gpx.

t.gpx

Первая строка вывода :
XX022421 jn32766 Geocache | Geocache jn32766 jn_mapoti c

в порядке, но вторая строка отличается от ожидаемой.
jn32766 ss_old_687 Геокэш ss_old_687 roze

Я ожидаю XX0 начало.

Что не так с моим сценарием?

1 Ответ

0 голосов
/ 27 марта 2020

У меня есть проблема, чтобы поставить его как «код».

            <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <gpx version="1.1" creator="Locus Map, Android"
        xmlns="http://www.topografix.com/GPX/1/1"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
        xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
        xmlns:gpxtrkx="http://www.garmin.com/xmlschemas/TrackStatsExtension/v1"
        xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v2"
        xmlns:locus="http://www.locusmap.eu">
        <metadata>
        <desc>File with points/tracks from Locus Map/3.43.1</desc>
        </metadata>
        <wpt lat="49.849758" lon="13.833783">
        <time>2019-09-30T22:00:00.000Z</time>
        <name>XX022421</name>
        <desc><![CDATA[jn32766 by jn_mapotic (1/1)]]></desc>
        <link href="https://www.geocaching.com/seek/cache_details.aspx?wp=">
        <text>jn32766</text>
        </link>
        <sym>Geocache</sym>
        <type>Geocache|Geocache</type>
        <groundspeak:cache id="293" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0/1">
        <groundspeak:name>jn32766</groundspeak:name>
        <groundspeak:placed_by>jn_mapotic</groundspeak:placed_by>
        <groundspeak:owner></groundspeak:owner>
        <groundspeak:type>Geocache</groundspeak:type>
        </groundspeak:cache>
        </wpt>
        <wpt lat="49.850607" lon="13.830060">
        <time>2020-02-16T23:00:00.000Z</time>
        <name>XX027931</name>
        <desc><![CDATA[ss_old_687 by roze (1/1)]]></desc>
        <link href="https://www.geocaching.com/seek/cache_details.aspx?wp=">
        <text>ss_old_687</text>
        </link>
        <sym>Geocache</sym>
        <type>Geocache|Geocache</type>
        <groundspeak:cache id="645" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0/1">
        <groundspeak:name>ss_old_687</groundspeak:name>
        <groundspeak:placed_by>roze</groundspeak:placed_by>
        <groundspeak:owner></groundspeak:owner>
        <groundspeak:type>Geocache</groundspeak:type>
        </groundspeak:cache>
        </wpt>
        </gpx>
...