从RSS提要中,如何获取每个项目标签中所有内容的字符串?
输入示例(简体):
TestHello world1Hi there
Tue, 21 Nov 2011 20:10:10 +0000
Hello world2Good afternoon
Tue, 22 Nov 2011 20:10:10 +0000
Hello world3blue paint
Tue, 23 Nov 2011 20:10:10 +0000
我需要一个使用此RSS文件的python函数(我现在正在使用beautifulsoup),并具有遍历每个项目的循环.我需要一个在每个项目中都包含所有字符串的变量.
第一个循环结果示例:
Hello world1Hi there
Tue, 21 Nov 2011 20:10:10 +0000
这段代码使我得到了第一个结果,但是我如何得到所有下一个?
html_data = BeautifulSoup(xml)
print html_data.channel.item
解决方法:
import BeautifulSoup
doc = BeautifulSoup.BeautifulStoneSoup(xml)
for item in doc.findAll('item'):
for elt in item:
if isinstance(elt,BeautifulSoup.Tag):
print(elt)
这就是您可以使用lxml做相同的事情的方法(出于某种原因,我发现它更容易使用):
import lxml.etree as ET
doc = ET.fromstring(xml)
for item in doc.xpath('//item'):
for elt in item.xpath('descendant::*'):
print(ET.tostring(elt))
标签:python,beautifulsoup,rss
来源: https://codeday.me/bug/20191014/1912464.html
本文介绍了一种使用Python的BeautifulSoup和lxml库来解析RSS提要的方法。通过示例代码展示了如何提取RSS项中的所有字符串内容,这对于后续的数据处理非常有用。

1万+

被折叠的 条评论
为什么被折叠?



