Searching specific attributes of tags

Collaborate on cutting-edge hong kong data technologies and solutions.
Post Reply
hasibaakterss3309
Posts: 762
Joined: Thu Jan 02, 2025 7:46 am

Searching specific attributes of tags

Post by hasibaakterss3309 »

STEP 7. ACCESSING SPECIFIC TAGS ATTRIBUTES
We learned that a tag can have more than one nested tag. For example, if we run soup . header . div , the <div> will have many nested tags. Note that we are only calling the <div> inside <header>, so the other tag inside <header> will not be shown.


As we can see, there are many attributes in one tag and the question is how to access only the attribute we need. BeautifulSoup has a function ' find ' and ' find _ all ' . To make it clearer, we will show you how to use both functions and how they differ from each other. As an example, we will find the price of each product. To see the code of the price element, just hover over the price indicator.

After moving the cursor we can determine that france telegram database the price is in the h4 tag , the class value is pull-right price.

Price Indicator
Next we want to find the string of the h4 element using the find function:

#
soup.find(‘h4’, class_= ‘pull-right price’)

Result:

Out[28]: <h4 class=”pull-right price”>$295.99</h4>

As you can see, $295.99 is the h4 attribute (string). But what happens if we use find_all.
Post Reply