Skip to main content
 首页 » 编程设计

python之RSS/Python之解析单个图像 URL

2025年05月04日9dflying

我正在学习如何正确解析 xml 和 rss 提要,但遇到了一个小问题。我在 python 中使用 feedbarser 来解析 RSS 提要中的特定条目,但无法弄清楚如何从内容部分解析单个 img src。

这是我目前所拥有的。

import dirFeedparser.feedparser as feedparser 
 
feedurl = feedparser.parse('http://dustinheroin.chompblog.com/index.php?cat=22&feed=rss2') 
statusupdate = feedurl.entries[0].content 
 
print statusupdate 

现在,当我打印内容时,我得到了这个:

[{'base': u'http://dustinheroin.chompblog.com/index.php?cat=22&feed=rss2', 'type': u'text/html', 'value': u'<p><a href="http://dustinheroin.chompblog.com/wp-content/uploads/2012/01/20120129-154945.jpg"><img alt="20120129-154945.jpg" class="alignnone size-full" src="http://dustinheroin.chompblog.com/wp-content/uploads/2012/01/20120129-154945.jpg" /></a></p>', 'language': None}] 

从中获取 IMG SRC 的最佳方法是什么?感谢任何帮助,谢谢!

请您参考如下方法:

你也可以试试lxml .使用 lxml,您可以使用 xpath expressions .

这里的 x 是你的状态更新。

from lxml import etree 
st = x[0]["value"] 
doc = etree.fromstring(st) 
value = doc.xpath("//img/@src") #xpath expr = //img/@src 
"".join(value) 

Output = 'http://dustinheroin.chompblog.com/wp-content/uploads/2012/01/20120129-154945.jpg'