Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract element with no class attribute

I need to navigate to an html element of a particular type. However, there are many such elements of that type on the page, with many different classes.

I need one which does not have any class attribute.

Should I look for one with class == '', or is there some other way?

like image 703
MaxYarmolinsky Avatar asked Jan 30 '12 08:01

MaxYarmolinsky


2 Answers

Use

soup.findAll(attrs={'class': None}) 

Quoting from docs:

You can use attrs if you need to put restrictions on attributes whose names are Python reserved words, like class, for, or import; or attributes whose names are non-keyword arguments to the Beautiful Soup search methods: name, recursive, limit, text, or attrs itself.

like image 111
soulcheck Avatar answered Oct 04 '22 09:10

soulcheck


As of Beautiful Soup version 4.1.2, you can use the class_ keyword argument.

To select an element without a class attribute, you can just specify None:

soup.find_all(class_=None) 

Alternatively, specifying False also works:

soup.find_all(class_=False) 
like image 26
Josh Crozier Avatar answered Oct 04 '22 11:10

Josh Crozier