gpt4 book ai didi

python - 如何提取和忽略标记中的跨度? - Python

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:02 25 4
gpt4 key购买 nike

如何提取和忽略 HTML 标记中的跨度?

我的输入是这样的:

<ul class="definitions">
<li><span>noun</span> the joining together of businesses which deal with different stages in the production or <a href="sale.html">sale</a> of the same <u slug="product">product</u>, as when a restaurant <a href="chain.html">chain</a> takes over a <a href="wine.html">wine</a> importer</li></ul>

期望的输出:

label = 'noun' # String embedded between <span>...</span>
meaning = 'the joining together of businesses which deal with different stages in the production or sale of the same product, as when a restaurant chain takes over a wine importer' # the text without the string embedded within <span>...</span>
related_to = ['sale', 'chain', 'wine'] # String embedded between <a>...</a>
utag = ['product'] # String embedded between <u>...</u>

我已经试过了:

>>> from bs4 import BeautifulSoup
>>> text = '''<ul class="definitions">
... <li><span>noun</span> the joining together of businesses which deal with different stages in the production or <a href="sale.html">sale</a> of the same <u slug="product">product</u>, as when a restaurant <a href="chain.html">chain</a> takes over a <a href="wine.html">wine</a> importer</li></ul>'''
>>> bsoup = BeautifulSoup(text)
>>> bsoup.text
u'\nnoun the joining together of businesses which deal with different stages in the production or sale of the same product, as when a restaurant chain takes over a wine importer'

# Getting the `label`
>>> label = bsoup.find('span')
>>> label
<span>noun</span>
>>> label = bsoup.find('span').text
>>> label
u'noun'

# Getting the text.
>>> bsoup.text.strip()
u'noun the joining together of businesses which deal with different stages in the production or sale of the same product, as when a restaurant chain takes over a wine importer'
>>> bsoup.text.strip
>>> definition = bsoup.text.strip()
>>> definition = definition.partition(' ')[2] if definition.split()[0] == label else definition
>>> definition
u'the joining together of businesses which deal with different stages in the production or sale of the same product, as when a restaurant chain takes over a wine importer'

# Getting the related_to and utag
>>> related_to = [r.text for r in bsoup.find_all('a')]
>>> related_to
[u'sale', u'chain', u'wine']
>>> related_to = [r.text for r in bsoup.find_all('u')]
>>> related_to = [r.text for r in bsoup.find_all('a')]
>>> utag = [r.text for r in bsoup.find_all('u')]
>>> related_to
[u'sale', u'chain', u'wine']
>>> utag
[u'product']

使用 BeautifulSoup 没问题,但获取所需内容有点冗长。

是否有其他方法可以实现相同的输出?

某些组是否有正则表达式方法来捕获所需的输出?

最佳答案

它仍然具有漂亮的结构良好,并且您已经清楚地说明了规则集。我仍然会使用 BeautifulSoup 应用“Extract Method”重构方法来处理它:

from pprint import pprint
from bs4 import BeautifulSoup


data = """
<ul class="definitions">
<li><span>noun</span> the joining together of businesses which deal with different stages in the production or <a href="sale.html">sale</a> of the same <u slug="product">product</u>, as when a restaurant <a href="chain.html">chain</a> takes over a <a href="wine.html">wine</a> importer</li></ul>
"""

def get_info(elm):
label = elm.find("span")
return {
"label": label.text,
"meaning": "".join(getattr(sibling, "text", sibling) for sibling in label.next_siblings).strip(),
"related_to": [a.text for a in elm.find_all("a")],
"utag": [u.text for u in elm.find_all("u")]
}

soup = BeautifulSoup(data, "html.parser")
pprint(get_info(soup.li))

打印:

{'label': u'noun',
'meaning': u'the joining together of businesses which deal with different stages in the production or sale of the same product, as when a restaurant chain takes over a wine importer',
'related_to': [u'sale', u'chain', u'wine'],
'utag': [u'product']}

关于python - 如何提取和忽略标记中的跨度? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32720287/

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