BeautifulSoup findAll() given multiple classes?

sebo picture sebo · Sep 10, 2013 · Viewed 71.7k times · Source

I would like to scrape a list of items from a website, and preserve the order that they are presented in. These items are organized in a table, but they can be one of two different classes (in random order).

Is there any way to provide multiple classes and have BeautifulSoup4 find all items which are in any of the given classes?

I need to achieve what this code does, except preserve the order of items as it was in the source code:

items = soup.findAll(True,{'class':'class1'})
items += soup.findAll(True,{'class':'class2'})

Answer

Roman Pekar picture Roman Pekar · Sep 10, 2013

you can do this

soup.findAll(True, {'class':['class1', 'class2']})

example:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<html><body><div class="class1"></div><div class="class2"></div><div class="class3"></div></body></html>')
>>> soup.findAll(True, {"class":["class1", "class2"]})
[<div class="class1"></div>, <div class="class2"></div>]