未找到Findnext。

各位专家,大家好!
我在YouTube上找到了这个程序,并试图使用XML文件找出去往北纬和北纬靠近Dave的公交车,但它显示了一个错误...
Lat=Float(Bus.findNext('lat'))
AttributeError:'xml.etree.ElementTree.Element'对象没有属性'findNext'
代码是这样的,

选择 | 换行 | 行号
  1. import urllib
  2.  
  3. daves_latitude = 41.98062
  4. daves_longitude = -87.668452
  5.  
  6. from xml.etree.ElementTree import parse
  7. doc = parse('rt22.xml')
  8.  
  9. for bus in doc.findall('bus'):
  10.     lat = float(bus.findnext('lat'))
  11.     if lat > daves_latitude:
  12.         direction = bus.findnext('d')
  13.         if direction.startwith('North'):
  14.             busid = bus.findnext('id')
  15.             print (busid,lat)
  16.  

以下是XML文件链接。
Http://ctabustracker.com/bustime/map...e.jsp?route=22
提前谢谢你..

# 回答1


正如回溯显示的那样,
公共汽车
没有"findNext"方法。它有一个返回列表的"findall"方法。

选择 | 换行 | 行号
  1. from xml.etree.ElementTree import parse
  2.  
  3. daves_latitude = 41.98062
  4. daves_longitude = -87.668452
  5.  
  6. doc = parse("rt22.xml")
  7.  
  8. for bus in doc.findall('bus'):
  9.     lat = float(bus.findall('lat')[0].text)
  10.     if lat > daves_latitude:
  11.         direction = bus.findall('d')[0].text
  12.         if direction.startswith('North'):
  13.             busid = bus.findall('id')[0]
  14.             print (busid,lat)

标签: python

添加新评论