2016-09-22 206 views
2

我想在Django中使用美麗的湯來提取xml標籤。這是我正在使用的標記示例:如何使用美麗湯從xml標籤中提取屬性?

<item> 
<title> 
Title goes here 
</title> 
<link> 
Link1 goes here 
</link> 
<description> 
Description goes here 
</description> 
<media:thumbnail url="Image URL goes here" height="222" width="300"/> 
<pubDate>Thu, 15 Sep 2016 13:24:48 EDT</pubDate> 
<guid isPermaLink="true"> 
Link2 goes here 
</guid> 
</item> 

我已經獲得標題,鏈接和描述標記的字符串。但是我無法從media:thumbnail標籤獲取網址。

這是我得到的標籤的其餘部分的值的片段:

soup=BeautifulSoup(urlopen(xmllink),'xml') 
for items in soup.find_all('item'): 
    listTitle.append(items.title.get_text()) 
    listURL.append(items.link.get_text()) 
    listDescription.append(items.description.get_text()) 

幫助

+1

向我們展示您到目前爲止嘗試過的方法。 –

回答

2

的問題是,因爲不是每一個項目都有一個媒體:縮略圖所以你需要檢查第一:

In [60]: import requests 

In [61]: from bs4 import BeautifulSoup 

In [62]: soup = BeautifulSoup(requests.get("https://rss.sciencedaily.com/computers_math/computer_programming.xml").content, "xml") 

In [63]: 

In [63]: for item in soup.find_all("item"): 
    ....:   thumb = item.find("thumbnail") 
    ....:   if thumb: 
    ....:     print(thumb["url"]) 
    ....:   
https://images.sciencedaily.com/2016/09/160915132448.jpg 
https://images.sciencedaily.com/2016/09/160915090018.jpg 
https://images.sciencedaily.com/2016/09/160914090327.jpg 
https://images.sciencedaily.com/2016/09/160913134149.jpg 
https://images.sciencedaily.com/2016/09/160909094844.jpg 
https://images.sciencedaily.com/2016/09/160907125004.jpg 
https://images.sciencedaily.com/2016/09/160906085157.jpg 
https://images.sciencedaily.com/2016/08/160831085055.jpg 
https://images.sciencedaily.com/2016/08/160822181811.jpg 
https://images.sciencedaily.com/2016/08/160815134941.jpg 
https://images.sciencedaily.com/2016/08/160815134817.jpg 
https://images.sciencedaily.com/2016/08/160809095640.jpg 
https://images.sciencedaily.com/2016/08/160803140137.jpg 
https://images.sciencedaily.com/2016/07/160722104135.jpg 
https://images.sciencedaily.com/2016/07/160721144139.jpg 
https://images.sciencedaily.com/2016/07/160721103855.jpg 
https://images.sciencedaily.com/2016/07/160720094641.jpg 
https://images.sciencedaily.com/2016/07/160718133206.jpg 
https://images.sciencedaily.com/2016/07/160713105850.jpg 
https://images.sciencedaily.com/2016/07/160711151055.jpg 
https://images.sciencedaily.com/2016/07/160707083258.jpg 
https://images.sciencedaily.com/2016/06/160629125823.jpg 
https://images.sciencedaily.com/2016/06/160627125140.jpg 
https://images.sciencedaily.com/2016/06/160624101050.jpg 
https://images.sciencedaily.com/2016/06/160622104810.jpg 

更快的替代方案將是使用LXML

from lxml import etree 

for item in tree.findall(".//item/media:thumbnail",tree.nsmap): 
    parent = item.getparent() 
    print(parent.xpath("title/text()")[0]) 
    print(parent.xpath("link/text()")[0]) 
    print(item.get("url")) 
+0

非常感謝!但是由於我在Django項目中,'listImage.append(items.find(「media:thumbnail」)[「url」])'拋出了一個TypeError(「'NoneType'對象不是可以下載的」), '錯誤。 Wihtout這條線,其他的一切都很完美 –

+0

我使用的XML解析器 –

+0

它仍然拋出相同的錯誤夥伴 –