我有这个脚本:
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。
输出的第一行:XX022421 jn32766 Geocache |地理缓存jn32766 jn_mapotic
可以,但是第二行与我预期的不同。jn32766 ss_old_687 Geocache ss_old_687 roze
我希望开头是XX027931。
我的脚本怎么了?
我在像“代码”这样放置时有问题。
<?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>