如何使用Beautiful Soup从xml标签中提取属性?

时间:2016-09-22 14:19:55

标签: python xml attributes beautifulsoup

我正在尝试在Django中使用Beautiful Soup来提取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 个答案:

答案 0 :(得分:2)

问题是因为并非所有项目都有 media:thumbnail 所以您需要先检查:

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"))