gpt4 book ai didi

python - BeautifulSoup逐行输出提取的文本

转载 作者:行者123 更新时间:2023-12-05 05:05:45 27 4
gpt4 key购买 nike

下面的 html 示例,我正在使用 BeautifulSoup 来提取文本。

txt = """[<dd class="qs" id="qsff"><br/>Pretty women wonder where my secret lies. <br/>I'm not cute or built to suit a fashion model's size<br/>But when I start to tell them,<br/>They think I'm telling lies.<br/><br/>I say,<br/>It's in the reach of my arms<br/>The span of my hips,<br/>The stride of my step,<br/>The curl of my lips.<br/><br/></dd>]"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(txt, "lxml")

for node in soup:
print (node.text)

# [Pretty women wonder where my secret lies. I'm not cute or built to suit a fashion model's sizeBut when I start to tell them,They think I'm telling lies.I say,It's in the reach of my armsThe span of my hips,The stride of my step,The curl of my lips.]

它向我展示了上面的整个字符串 block ,但我想逐行显示它们,例如:

Pretty women wonder where my secret lies.
I'm not cute or built to suit a fashion model's size
But when I start to tell them,
....

我在下面尝试过,但它不起作用。

for node in soup.find_all('br'):
print (node.text)

逐行输出它们的正确方法是什么?谢谢。

最佳答案

遍历字符串,而不是节点:

for node in soup.dd.strings:
print(node)
#Pretty women wonder where my secret lies.
#I'm not cute or built to suit a fashion model's size
#But when I start to tell them,
#....

为什么要将文本括在方括号中?

关于python - BeautifulSoup逐行输出提取的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60370520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com