【发布时间】:2014-01-17 05:10:04
【问题描述】:
我的代码如下:
s = """<P><A>This is the topic</A>
This is the text</P>
<P> </P>
<P><A>Another Topic</A>:
Another Text </P>"""
for s in soup.findAll('a'):
print s.text
输出是:
This is the topic
Another Topic
我想得到“这是文本”和“另一个文本”。但不知何故我不能使用这段代码。条件是我必须使用 for 循环。因此,如果有人知道如何提取所需的文本,那将是非常有帮助的。
【问题讨论】:
-
谢谢大家。我尝试在 soup.findAll('a') 中使用 - for s: print s.parent.text ...它起作用了
-
使用 s.parent.text 我获得了所有段落的所有文本,如下所示:This is the topicThis is the text