Home > Software engineering >  How can I access this div statement with Beautiful Soup?
How can I access this div statement with Beautiful Soup?

Time:11-14

First of all, I am not a coder so sorry in advance for a possible bad explanation.

I want to retrieve the html code inside the following div statement using Beautiful soup:

<div x-y-z> == $0

I usually would retrieve html code in soup like the following:

html = soup.find("div", class_="x-y-z")

My problem here is that there is no class or id or anything included in the div statement, only the "x-y-z" of which I dont know what it is.

How can I retrieve the html code from the above-shown div with Beautiful soup? I really appreciate any help you can provide.

CodePudding user response:

You can use CSS selector:

from bs4 import BeautifulSoup

soup = BeautifulSoup("<div x-y-z>Something</div>", "html.parser")

print(soup.select_one("div[x-y-z]"))

Prints:

<div x-y-z="">Something</div>

Or bs4 API:

print(soup.find("div", {"x-y-z": True}))

Prints:

<div x-y-z="">Something</div>
  • Related