`Can Beautiful Soup extract data (text) from elements that share the same class identifier?`

Currently working on a personal project focused on web scraping, using beautiful soup to extract data from a website. Encountered an issue where data with the same class but different attributes is present. For instance:

<div class="pi--secondary-price">
<span class="pi--price">$11.99 /<abbr title="Kilogram">kg</abbr></span>
<span class="pi--price">$5.44 /<abbr title="Pound">lb.</abbr></span>
</div>

The challenge is to extract only $11.99/kg. Currently, both prices are being retrieved as: $11.99 /kg $5.44 /lb.

Attempted x.select('.pi--secondary-price') which returns both prices. How can I isolate and extract just one price ($11.99 /kg)?

Answer №1

To start, locate the <abbr> element and then find its parent tag using the following method:

from bs4 import BeautifulSoup

html = '''
<div class="pi--secondary-price">
<span class="pi--price">$11.99 /<abbr title="Kilogram">kg</abbr></span>
<span class="pi--price">$5.44 /<abbr title="Pound">lb.</abbr></span>
</div>
'''

soup = BeautifulSoup(html, 'html.parser')

kg = soup.find(title="Kilogram")
print(kg.parent.text)

This code will output $11.99 /kg. For further details, refer to the BeautifulSoup documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A simple lambda expression shortcut technique for sorting elements in Python

I have a functional code snippet but it's quite messy: a = 0 for k in keys: a = a + 1 if a == 1: k1 = k if a == 2: k2 = k if a == 3: k3 = k ...

unleashing the power of Gremlin server with OrientDB

As someone who is new to using the gremlin-server and orientDB on Ubuntu, I am interested in connecting the two. Can anyone provide information on the requirements for establishing this connection? I have already successfully connected the gremlin-server ...

Challenges arising from the use of 'position: absolute' in managing relationships between grandparents, parents, and children

Within my React project, all code is enclosed within the root div (grandparent). In my Project.js component, there is a separate div (parent) solely responsible for the background color. This component is then displayed in App.js without any additional d ...

What is the most efficient way to convert an entire HTML page into a different language using Django?

As I work on building a website with Django framework, I am faced with the task of translating my web page into French while keeping all other pages in English. <html lang="fr"> Despite setting the code as shown above, I am encountering issues wher ...

Issue encountered: Module not found error persists even though the module is installed and appears in the list generated by pip

After installing biopython and related modules, I attempted to load them with the following code: from BCBio.GFF import GFFExaminer import pprint from BCBio import GFF However, I encountered the following error: ----------------------------------------- ...

Exploring the Possibilities of Automating React Material UI Input Fields with Selenium

Seeking guidance on automating data entry for media files post upload. Using Selenium to navigate the media library, but stuck on sending input to fields. The website is built with React Material UI, requiring xpath use exclusively, which is effective so ...

The Pandas DataFrame is displaying cells as strings, but encountered an error when attempting to split the cells

I am encountering an issue with a Pandas DataFrame df. There is a column df['auc_all'] that contains tuples with two values (e.g. (0.54, 0.044)) Initially, when I check the type using: type(df['auc_all'][0]) >>> str However, ...

What is the method for HTML inline handlers to retrieve the global window object and the variables contained within it?

During my coding test, I encountered an interesting scenario. I had a function called write and used a button with an inline onclick handler to trigger the write() function. function write(text) { alert(text) } <button onclick='write("Some tex ...

What steps can I take to direct mobile visitors to the mobile-friendly version of my website?

Looking for a way to automatically redirect users on mobile devices from www.domain.com to the new mobile version at m.domain.com? ...

The submit button click does not trigger the execution of ajax code

I'm faced with a challenge when trying to implement an ajax call in my form. My goal is to display a spinner on the screen to indicate that the form is processing and submitting information. However, it seems that the ajax call is not being triggered ...

Assemblage of fields within a Django Field

Click here for image description class Car(models.Model): stockid = models.AutoField(primary_key=True) galleryIndex = models.IntegerField() title = make + model + year make = models.CharField(max_length=50) model = models.CharField(ma ...

Wheelnav.js implementing a dynamic bouncing animation

I'm currently in the process of working on a pie menu with wheelnav.js and everything is going smoothly so far. However, I can't seem to find any information in the wheelnav.js documentation on how to eliminate the bouncing effect when a menu cho ...

Troubleshooting Problems with Positioning and Floating in HTML and CSS

I'm facing some challenges with clearing floats (or it could be something else?). My goal is to have the #newsbar div free from the previous floats so that its width can extend 100% across the page/browser. I believe I've tried everything within ...

I'm confused why this code is functioning in JSFiddle but not on my HTML webpage

For the first time, I am encountering this issue. I am currently attempting to integrate this code into my application http://jsfiddle.net/TC6Gr/119/ My attempts include: Pasting all the jsfiddle code in a new page without my code, but it doesn't w ...

How can I align two div buttons at the top and bottom to appear on the left and right sides?

How can I change the position of two buttons from top and bottom to left and right as shown in the second image? I am utilizing Floating Vue, so the buttons will be contained within a div. Is it feasible to adjust their position accordingly? Check out th ...

What is the best way to break apart the word "ActionAction-AdventureShooterStealth" into individual words?

There is a column called genres in the games dataset which contains all the genres without any spaces or special characters. The major genre is listed first followed by other genres. Refer to the table below for better understanding: ...

Show a dynamic dropdown box using AJAX if a specific variable is present

I am currently working on implementing an ajax call for a dynamic dropdown box. Once the variable $place is available, it triggers an ajax call to populate the dropdown box. My goal is to pass the variable $place to listplace.php, encode it as JSON data ...

What is the best way to create an arrow that tracks the browser window, but only goes as far as the end of its designated container

Currently, I am facing a challenge in implementing a scroll down arrow. While this is typically a simple task, it has proven to be a bit more complex this time around. The complexity arises from the fact that my customer desires a homepage with an image of ...

Choosing different elements using identical classes in JQuery

Struggling with a coding problem that seems like it should be an easy fix, but can't quite figure it out. The HTML code I have is as follows: <section class="actualite"> <div class="actualite-text"> <h3 class="title"&g ...

Exploring the world of CSS and designing layouts using multiple div elements

Upon reviewing this website, I must admit it is getting close to what I envision. However, being a perfectionist and new to HTML and CSS, I find myself struggling to achieve the desired outcome. The layout consists of two divs named topred and top yellow, ...