我会避免使用nextSibling,因为从您的问题开始,您想要包括直到next的所有内容
<a>,而不管它是在同级元素,父元素还是子元素中。
因此,我认为最好的方法是找到下一个
<a>元素的节点,然后递归循环,直到出现为止,然后添加遇到的每个字符串。如果您的HTML与示例大不相同,则可能需要整理以下内容,但是这样的工作应该可以:
from bs4 import BeautifulSoup#by taking the `html` variable from the question.html = BeautifulSoup(html)firstBigTag = html.find_all('big')[0]nextATag = firstBigTag.find_next('a')def loopUntilA(text, firstElement): text += firstElement.string if (firstElement.next.next == nextATag): return text else: #Using double next to skip the string nodes themselves return loopUntilA(text, firstElement.next.next)targetString = loopUntilA('', firstBigTag)print targetString


