python_BeautifulSoup库的find和find_all函数中出现class_

因为class是python的保留关键字,若要匹配标签内class的属性,需要特殊的方法,有以下两种:

在attrs属性用字典的方式进行参数传递
BeautifulSoup自带的特别关键字class_

# 第一种:在attrs属性用字典进行传递参数
find_class = soup.find(attrs={'class':'item-1'})
print('findclass:',find_class,'\n')
# 第二种:BeautifulSoup中的特别关键字参数class_
beautifulsoup = soup.find(class_ = 'item-1')
print('BeautifulSoup_class_:',beautifulsoup_class_,'\n')

版权声明:本文为dmxbb原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。