【问题标题】:Using BeautifulSoup, how to get text only from the specific selector without the text in the children?使用 BeautifulSoup,如何仅从特定选择器中获取文本而没有子项中的文本?
【发布时间】:2017-02-06 17:21:03
【问题描述】:
我不知道如何对BeautifulSoup 进行编码,以便它只给我所选标签中的文本。我得到更多诸如它的孩子(ren)的文字!
例如:
from bs4 import BeautifulSoup
soup = BeautifulSoup('<div id="left"><ul><li>"I want this text"<a href="someurl.com"> I don\'t want this text</a><p>I don\'t want this either</li><li>"Good"<a href="someurl.com"> Not Good</a><p> Not Good either</li></ul></div>', "html5lib")
x = soup.select('ul > li')
for i in x:
print(i.text)
输出:
“我想要这个文本”我不想要这个文本我也不想要这个
“好”不好也不好
期望的输出:
“我想要这段文字”
“好”
【问题讨论】:
标签:
python
web-scraping
beautifulsoup
html-parsing
【解决方案1】:
一种选择是获取contents list 的第一个元素:
for i in x:
print(i.contents[0])
另一个——找到第一个文本节点:
for i in x:
print(i.find(text=True))
两者都会打印:
"I want this text"
"Good"
【解决方案2】:
from bs4 import BeautifulSoup
from bs4 import NavigableString
soup = BeautifulSoup('<div id="left"><ul><li>"I want this text"<a href="someurl.com"> I don\'t want this text</a><p>I don\'t want this either</li><li>"Good"<a href="someurl.com"> Not Good</a><p> Not Good either</li></ul></div>', "html5lib")
x = soup.select('ul > li')
for i in x:
if isinstance(i.next_element, NavigableString):#if li's next child is a string
print(i.next_element)