Python 美化组以逐行输出提取的文本
下面是html示例,我正在使用BeautifulSoup提取文本Python 美化组以逐行输出提取的文本,python,beautifulsoup,Python,Beautifulsoup,下面是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 thi
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.]
我在下面试过了,但没用
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,
#....
为什么要将文本括在方括号中?
print(soup.dd.get_text(separator='\n'))
@PhungDuyPhong,谢谢。它起作用了!非常感谢。因为它是以前提取的。:)
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,
#....