您可以使用递归函数迭代id='dictionary-neodict-es'的最外层div,以说明存在多个{}类为indent FyTYr的嵌套{}:from bs4 import BeautifulSoup as soup
import requests, bs4
def has_class(d, c):
return any(c in i.attrs.get('class', []) or has_class(getattr(i, 'contents', []), c) for i in d if i != '\n' and not isinstance(i, bs4.NavigableString))
def get_sentences(d):
if 'indent FyTYr' in d.attrs.get('class', []) and not has_class(d.contents, 'indent FyTYr'):
yield [d.div.span.text, d.div.em.text]
else:
for i in filter(lambda x:x != '\n' and not isinstance(x, bs4.NavigableString), getattr(d, 'contents', [])):
yield from get_sentences(i)
result = list(get_sentences(soup(requests.get('https://www.spanishdict.com/translate/rojo').text, 'html.parser').find('div', {'id':'dictionary-neodict-es'})))
现在,您可以访问所有句子:
^{pr2}$
要访问所需的字符串:
^{3}$
输出:'The sky turned red at sundown.'