如果在使用BeautifulSoup解析html时需要按照class查找元素,但是某些元素的class属性为空,可以使用以下方法解决:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
result = soup.select('#element_id')
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
element = soup.find('div', {'class': None})
if element is not None:
# do something with element