Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I access this div statement with Beautiful Soup?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading