[英]Parsing/Extracting Data from API XML feed with Python and Beautiful Soup
此處的Python / xml newb與Python和BeautifulSoup一起在嘗試學習如何解析XML,特別是與Oodle.com API混淆以列出汽車分類。 我已經在使用簡單的XML和BS方面取得了成功,但是無論如何嘗試,我似乎都無法獲得想要的數據。 我嘗試閱讀Soup文檔數小時,無法弄清楚。 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發出請求,以獲取供稿並保存到本地文件。 然后:
xml = open("temp.xml", "r")
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(xml)
那我不確定。 我已經嘗試了很多方法,但是所有事情似乎都比我想要的要糟得多,這使查找問題變得困難。 我正在嘗試獲取ID,標題,里程,價格,年份,年份。 那么,如何獲取這些信息並通過循環加快過程呢? 理想情況下,我想要一個for循環,例如:
for soup.listings.element in soup.listings:
id = soup.listings.element.id
...
我知道這顯然不起作用,但是有些東西會獲取列表的信息,然后將其存儲到列表中,然后移至下一個廣告。 感謝幫助人員
您可以執行以下操作:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.