因此,我創建了一個數組中的離開航班列表,以便在稍後的應用程序中進行迭代。Python沒有創建一個新的乾淨實例?
我從長長的html中獲取信息。
xpathes檢出和其他一切都很好。我有一個名爲FlightLegs的主要包裝對象。還有一個名爲Flights的小內部對象。
結構簡單。每個FlightLegs都有一個DATE,在這個DATE上,這些航班將在該日期的不同時間離開,而三個航班將以不同的時間離開。
所以就像你有一個日期:2012年3月28日和三個航班離開:8點10分,14點30分,20點。
夠簡單。
所以我有兩個嵌套循環。首先,通過FlightLegs循環。設置DATE ...然後在該航段中的航班上循環。並將它們添加到航班數組中。然後將FlightLegs廣告列表。並開始一個新的。
下面是代碼:
#Looping thourght the flight legs.
for flightLeg in flightLegs:
#. is needed in the xpath so it matches items only in this flight leg.
fleg = FlightLeg()
fleg.Date = str(flightLeg.xpath(".//input[@name='departDate1']")[0].get('value'))
innerFlights = flightLeg.xpath(".//div[@class='flights_flight']")
counter = 0
#Getting the three flights in the flight leg leaving at 8 - 14 - 20.
for flight in innerFlights:
fl = Flight()
fl.FlightPrice = str(flight.xpath(".//span[@class='flights_price']")[0].text)
fl.FlightDepartureTime = str(flight.xpath(".//span[@class='flights_departuretime']")[0].text)
fl.FlightArrivalTime = str(flight.xpath(".//span[@class='flights_arrivaltime']")[0].text)
fl.FlightNumber = str(flight.xpath(".//span[@class='flights_flightnumber']")[0].text)
fl.FlightDepAirport = str(flight.xpath(".//span[@class='flights_departureairport']")[0].text)
fleg.Flights.append(fl)
print "Lengts of inner flights: " + str(len(fleg.Flights))
counter += 1
print "Lengts of inner counter: " + str(counter)
flightList.append(fleg)
現在有了這個問題,在內部飛行航段陣列實際上是在3增加,直到最後飛行600似乎沒有新對象在循環中創建...所以會導致這種情況?
編輯:
FlightLeg是:
class FlightLeg:
Flight = []
Date = ""
航班是:
class Flight:
FlightPrice = ""
FlightDepartureTime = ""
.
.
.
順便說一句..我是新來的,當然了Python。我只是在學習。但是現在看到DSM-s的評論,它可能是靜態的。我認爲變量將默認爲public和instance。
我如何訪問它們並不重要。重要的部分是櫃檯。 ;-)
我會通過foreach訪問它們,例如。
究竟哪一行受到影響?什麼是輸出? – Marcin 2012-03-30 16:22:35
您實際上並未展示FlightLeg.Flights的定義方式。它可能是一個類變量,而不是一個實例變量,所以每個FlightLeg實例只共享一個Flights數組? – DSM 2012-03-30 16:22:53
我猜從第三行開始的所有代碼都縮進了四個空格?你能修好縮進嗎? – kossmoboleat 2012-03-30 16:27:12