Python 如何提取<;span>;在BeautifulSoup中,哪个包含普通文本以及其他HTML标记?
使用BeautifulSoup,我试图提取Python 如何提取<;span>;在BeautifulSoup中,哪个包含普通文本以及其他HTML标记?,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,使用BeautifulSoup,我试图提取标记之间的内容。我使用string属性来获得所需的输出。如果标记只包含文本,则可以正常工作。但是,如果标签中放置的不是普通文本,而是其他一些HTML标签,那么它就会失败。例如 如果我刮掉以下内容: <span>Elegant, Furnished, Planned</span> <span>Elegant, <b>Furnished</b>, Planned</span> 它工作
标记之间的内容。我使用string
属性来获得所需的输出。如果
标记只包含文本,则可以正常工作。但是,如果标签中放置的不是普通文本,而是其他一些HTML标签,那么它就会失败。例如
如果我刮掉以下内容:
<span>Elegant, Furnished, Planned</span>
<span>Elegant, <b>Furnished</b>, Planned</span>
它工作正常,我得到的输出如下:
Elegant, Furnished, Planned
但是,当我刮掉以下内容时,我得到了None
:
<span>Elegant, Furnished, Planned</span>
<span>Elegant, <b>Furnished</b>, Planned</span>
优雅、有家具、有计划
帮我弄清楚。我想你可以试试这个:
url = 'your.example.net'
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, "html.parser")
for span in soup.find_all('span'):
print (span.text)
它应该很好用。尝试使用lxml
from bs4 import BeautifulSoup as bs
html = '''
<span>Elegant, Furnished, Planned</span>
'''
soup = bs(html, 'lxml')
soup.select_one('span').text
从bs4导入美化组作为bs
html=“”
优雅的、有家具的、有计划的
'''
soup=bs(html,“lxml”)
汤。选择一个('span')。文本