Tags
Asked 3 years ago
1 Jul 2021
Views 613
Oral

Oral posted

BeautifulSoup find all title and href inside of div > span > a

BeautifulSoup find all title and href inside of div > span > a
web-api

web-api
answered May 1 '23 00:00

an example code using BeautifulSoup to find all title and href attributes inside of div > span > a :


from bs4 import BeautifulSoup

# assuming the HTML content is stored in the variable html
soup = BeautifulSoup(html, 'html.parser')

# find all div tags with class="my-class"
div_tags = soup.find_all('div', class_='my-class')

# iterate over each div tag and find all span tags inside it
for div in div_tags:
    span_tags = div.find_all('span')

    # iterate over each span tag and find all anchor tags inside it
    for span in span_tags:
        a_tags = span.find_all('a')

        # iterate over each anchor tag and print its title and href attributes
        for a in a_tags:
            print(a.get('title'), a.get('href'))

In this code, we first create a BeautifulSoup object from the HTML content using the html.parser parser . Then, we use the find_all method to find all div tags with class my-class. We iterate over each div tag and find all span tags inside it using find_all again. We iterate over each span tag and find all a tags inside it using find_all again. Finally, we iterate over each a tag and print its title and href attributes using the get method.
Post Answer