How to scrape tag with data-name?

Viewed 38

Hi I am trying to scrape a website.

I need to get some data from tag div data-name='dashboard-champ-content'.

r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")

for i in soup.find_all('div')["dashboard-champ-content"]:
    print(i)  # it`s not working

So, how can i do it?enter code here

1 Answers

If you have just one tag:

from bs4 import BeautifulSoup

html_doc = """
    <div data-name='dashboard-champ-content'>
        This I want
    </div>"""

soup = BeautifulSoup(html_doc, "html.parser")
print(
    soup.find("div", {"data-name": "dashboard-champ-content"}).get_text(
        strip=True
    )
)

Prints:

This I want

If multiple tags:

from bs4 import BeautifulSoup

html_doc = """
    <div data-name='dashboard-champ-content'>
        This I want 1
    </div>
    <div data-name='dashboard-champ-content'>
        This I want 2
    </div>"""

soup = BeautifulSoup(html_doc, "html.parser")
for div in soup.find_all("div", {"data-name": "dashboard-champ-content"}):
    print(div.get_text(strip=True))

Prints:

This I want 1
This I want 2
Related