0
URL = {URL here}
page = requests.get(URL, headers={header here})
soup = BeautifulSoup(page.content, 'html.parser')

container = soup.findAll("div", attrs={"class": "data1", "class": "data2"})"

Warning: Duplicate key 'class' in dictionary pylint(duplicate-key)

I wonder if it is possible to smash together "data1" and "data2" as one attribute?

Thanks in advance!

xqrdot
  • 13
  • 1
  • 2

2 Answers2

0

If you want to select element that has class data1 AND data2, you can use CSS selector:

data = '''
<div>
  <div class="data1">data1</div>
  <div class="data1 data2">data1 AND data2</div>
</div>
'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print(soup.select_one('div.data1.data2').text) # or select() to select multiple elements

Prints:

data1 AND data2
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

I would suggest use soup sieve, which come together with BeatifulSoup anyway

soup.select('div:is(.data1, .data2)')

check it out for more info: soup sieve

Sheng Zhuang
  • 647
  • 5
  • 10