2011-10-11 18 views
0

Python/xml newb在這裏玩弄Python和BeautifulSoup,試圖學習如何解析XML,特別是與Oodle.com API搞混以列出汽車分類。我用簡單的XML和BS取得了成功,但是在處理這個問題時,無論我嘗試什麼,我都無法獲得我想要的數據。我嘗試閱讀Soup文檔幾個小時,但無法弄清楚。 XML的結構是這樣的:使用Python和美味湯分析/提取API XML數據中的數據

<?xml version="1.0" encoding="utf-8"?> 
<oodle_response stat="ok"> 
    <current> 
     .... 
    </current> 
    <listings> 
     <element> 
      <id>8453458345</id> 
      <title>2009 Toyota Avalon XL Sedan 4D</title> 
      <body>...</body> 
      <url>...</url> 
      <images> 
       <element>...</element> 
       <element>...</element> 
      </images> 
      <attributes> 
       <features>...</features> 
       <mileage>32637</mileage> 
       <price>19999</price> 
       <trim>XL</trim> 
       <vin>9234234234234234</vin> 
       <year>2009</year> 
      </attributes> 
     </element>  
     <element>.. Next car here ..</element> 
     <element>..Aaaand next one here ..</element>  
    </listings> 
    <meta>...</meta> 
</oodle_response> 

我首先向urllib發送一個請求來抓取feed並保存到本地文件。然後:

xml = open("temp.xml", "r") 
from BeautifulSoup import BeautifulStoneSoup 
soup = BeautifulStoneSoup(xml) 

然後我不知道是什麼。我嘗試了很多東西,但一切似乎都會讓我想要的東西變得更加垃圾,並且很難找到問題。我只是想得到id,標題,里程,價格,年份,vin。那麼如何獲得這些信息並加快循環過程?理想的情況是我想要的是像循環:

for soup.listings.element in soup.listings: 
    id = soup.listings.element.id 
    ... 

我知道,沒有明顯的工作,但一些會爲上市獲取信息,並將其存儲到一個列表,然後移動到下一個廣告。感謝幫助傢伙

回答

0

你可以做這樣的事情:

for element in soup('element'): 
    id = element.id.text 
    mileage = element.attributes.mileage.text 
    price = element.attributes.price.text 
    year = element.attributes.year.text 
    vin = element.attributes.vin.text 
+0

感謝幫助! – user839924

+0

快速記下哦。我確實必須將第一行改爲'用於湯元素('列表')'不是湯('元素')。再次感謝。現在我似乎無法通過循環來繼續收集數據?它只爲第一個上市 – user839924