如何从 <li> 个元素中获取文本
How to get text from <li> elements
我有:
<ul>
<li>text1</li>
<li>text2 </li>
</ul>
现在我从 <li>
得到的文本是这样的:
result = page.css(' ul li').text
问题是,结果我得到一个没有空格的字符串,例如
text1text2
我想把它和<br>
分开,比如text1<br>text2<br>
。
我该怎么做?
来自“Searching a XML/HTML Document”
:
methods xpath and css actually return a NodeSet, which acts very much
like an array, and contains matching nodes from the document.
因此,如果您想连接来自所有 <li>
标签的所有文本,那么您应该像处理集合一样使用 css
方法结果:
page.css('ul li') # selects all li tags and returns collection of Node objects
.map(&:text) # maps collection of li nodes into array of corresponding texts
.join('<br>') # concatenates all nodes texts into a single string with <br> separator
我有:
<ul>
<li>text1</li>
<li>text2 </li>
</ul>
现在我从 <li>
得到的文本是这样的:
result = page.css(' ul li').text
问题是,结果我得到一个没有空格的字符串,例如
text1text2
我想把它和<br>
分开,比如text1<br>text2<br>
。
我该怎么做?
来自“Searching a XML/HTML Document” :
methods xpath and css actually return a NodeSet, which acts very much like an array, and contains matching nodes from the document.
因此,如果您想连接来自所有 <li>
标签的所有文本,那么您应该像处理集合一样使用 css
方法结果:
page.css('ul li') # selects all li tags and returns collection of Node objects
.map(&:text) # maps collection of li nodes into array of corresponding texts
.join('<br>') # concatenates all nodes texts into a single string with <br> separator