P粉8248896502023-08-14 13:21:33
You can definitely use BeautifulSoup to extract text from HTML tags that contain multiple words. In your example, you want to extract text from <a> tags that contain multi-word content. Here's how to achieve this using BeautifulSoup in Python.
from bs4 import BeautifulSoup html = ''' <div> <div> <a>hello there</a> <a>hi</a> </div> <a>what's up</a> <a>stackoverflow</a> </div> ''' soup = BeautifulSoup(html, 'html.parser') target_tags = soup.find_all('a') # 找到所有的<a>标签 multi_word_texts = [] for tag in target_tags: if ' ' in tag.get_text(): # 检查标签文本是否包含空格(表示多个单词) multi_word_texts.append(tag.get_text()) result = ' '.join(multi_word_texts) print(result)