user1406716
user1406716

Reputation: 9705

How to access text inside this <div class> that has no id? Using BeauitfulSoup

Below in the grayed out area is some text that I am trying to extract in a page.

I dont know how to access the text in the gray area. I tried the following but it did not work. The class does not have an id - how to get the text inside it?

comment = soup.find("div", {"class",  "GCARQJCDEXD"})

enter image description here

Upvotes: 1

Views: 40

Answers (1)

alecxe
alecxe

Reputation: 474071

You can locate the element by matching a class attribute to an empty string:

from bs4 import BeautifulSoup

data = """
<div class="GCARQJCDEXD">
    <div class="clearfix hidden">something here</div>
    <div class>
        desired text
    </div>
</div>
"""
soup = BeautifulSoup(data, "html.parser")

comment = soup.find("div", {"class": "GCARQJCDEXD"}).find("div", {"class": ""})
print(comment.get_text(strip=True))

Prints desired text.

Upvotes: 1

Related Questions