可以使用BeautifulSoup的find()
或find_all()
方法,再在参数中添加一个正则表达式,以识别包含特殊字符的元素名称。例如:
import re
from bs4 import BeautifulSoup
xml = 'greetings '
soup = BeautifulSoup(xml, 'xml')
special_name = re.compile('^hello-')
element = soup.find_all(name=special_name)
print(element)
运行结果将返回包含特殊字符名称的元素:
[greetings ]