0

I have a html where I want to get elements with class="a" and id="c d". If I have only one of them, I can use soup.select('[class="a"]') and soup.select('[id="c d"]').

Can we combine '[class="a"]' and '[id="c d"]' in the same command?

from bs4 import BeautifulSoup

# Example HTML
html = """
<ul class="a" id="c d">Content 1</ul>
<li class="b" id="c>Content 2</li>
<li class="a" id="d>Content 3</li>
"""

# Create BeautifulSoup object
soup = BeautifulSoup(html, 'html.parser')

soup.select('[id="c d"]')
soup.select('[class="a"]')
1
  • 1
    soup.select('[id="c d"].a') ? Commented May 25 at 13:43

1 Answer 1

0

I have found a solution:

soup.select('[class="a"][id="c d"]')

Another solution given in a comment is soup.select('[id="c d"].a').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.