Scrapy and xpath not functional - solely relying on css for operation

Attempting to scrape data from this website:

After retrieving all elements using the following code:

products = response.xpath("//ul[@class='products']//ancestor::li")

I attempted to extract prices for all elements in the scrapy shell. Initially, I used the following code:

>>> for p in products:
...  p.xpath("//span[@class='price']//child::bdi/text()").get()           
... 
'134.99'
'134.99'
'134.99'
'134.99'
'134.99'
'134.99'
'134.99'
'134.99'
'134.99'

It appears that only the first price is being retrieved despite the loop.

Then, I tried using CSS selectors and it worked as expected:

>>> for p in products:
...  p.css("span.price bdi::text").get()
... 
'134.99'
'49.99'
'39.99'
'39.99'
'39.99'
'129.99'
'24.99'
'49.99'
'119.99'

The xpath-selector approach seems to not work as intended. Why could this be happening?

Answer №1

When refining your xpath selection, remember to include .// in order to retrieve the specific outcome. Consider implementing the following:

p.xpath(".//span[@class='price']//child::bdi/text()").get() 

Answer №2

To apply xpath to an existing web element,

you can utilize the following code snippet:

for item in items:
    item.xpath(".//div[@class='info']//child::span/text()").get()

Answer №3

This specific xpath selector is compatible with Chrome as well.

//span[@class="woocommerce-Price-amount amount"]//child::bdi/text()

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

I am having trouble with the spacing on my website, even after I have tried declaring it

I'm having trouble formatting the footer to appear at the bottom of the page. I've tried adjusting the CSS code for the .container and .footer classes, but nothing seems to work. As a newbie in website development, any helpful tips or suggestions ...

Elements of <nav> within a <footer> section

I've recently started using HTML5 display elements like header, footer, and nav. While reading about the nav element in particular, I found this interesting information in the HTML5 spec: The nav element is primarily intended for major navigation b ...

Stacking sheets of hole-punched paper on top of one another, create a visually captivating

I am in search of a way to create those distinctive black dots with papers displayed here: body { background: linear-gradient(#ccc, #fff); font: 14px sans-serif; padding: 20px; } .letter { background: #fff; box-shadow: 0 0 10px rgba ...

`Modified regions determined by cursor location`

I am working with a split layout featuring two columns, and I need the ability to make each column separately scrollable. Due to using a specialized scroll-to function, I cannot use overflow-y: scroll; or overflow: auto;. I am looking for alternative solut ...

Mastering Puppeteer: Harnessing the Power of XPath and External Variables in the Evaluate Function

I'm currently facing an issue while attempting to utilize an XPath and an external variable within Puppeteer's evaluate function. The problem arises in the following scenarios: When I include the external variable in the evaluate function, I am ...

adjust bootstrap column width to fill the container

https://i.stack.imgur.com/tCuL6.png I am hoping that this image will provide a visual aid for what I am trying to convey. My goal is to have the arrow and the boxes aligned in a row without any empty space on the right side. As you can see, the leftmost b ...

The issue of Google fonts failing to load on Windows browsers while loading perfectly on Mac browsers has been observed

Stackers. I am currently the Front End developer for www.PinionLMS.com. Our website is using "Lato," a Google font. The issue we are experiencing is that when you access the site from a Windows (8.1) browser such as Chrome, Firefox, or Internet Explorer ...

The reason I am unable to extract information from the elements within the <ul> tag

I am currently attempting to extract reviews data from booking.com that is contained within the <ul> tag with the class attribute set to "review_list". There are a total of 10 reviews, each of which is inside an <li> element with the class attr ...

The dropdown menus in Bootstrap are expanding outside the screen when opened

When I click on the dropdown in the Navbar on the right side, the menus open up far to the right and are not visible until I scroll horizontally. Here is the code snippet: <nav class="navbar navbar-expand-lg navbar-light bg-light"> < ...

Text Parallax Effect

For my website, I am interested in incorporating a unique parallax effect. Instead of just fixing a background image and allowing scrolling over it, I want to apply this effect to all of the content on my site. The website consists of a single page with m ...

Utilize a dual-color gradient effect on separate words within the <li> element

I am attempting to display the fizz buzz function in an unordered list, with each word being a different color ('fizz'-- green, 'buzz'--blue) as shown here: https://i.sstatic.net/Yvdal.jpg I have successfully displayed "fizz" and "buz ...

The hover effect flickers in Firefox, but remains stable in Google Chrome

Here is an example link to check out: I am facing a dilemma on how to solve this issue and unsure if it is related to jQuery or the DOM structure. If you have any suggestions, they would be greatly appreciated. Thank you! ...

Restricting choices once user selects an option (HTML)

Currently, I am faced with a dilemma regarding two sets of options for an HTML form. One set consists of various units (such as cm, in, ft.), while the other set includes different locations. The selection of a unit would restrict certain location option ...

What causes conflicts between CSS and HTML tables?

I'm currently working on designing an email ticket template that incorporates a small table for clients to easily view the subject and description of their ticket. The template was looking great until the table was inserted, causing a complete formatt ...

Tips for animating the box-shadow effect using jQuery

Can anyone provide insight on how to animate the box-shadow of multiple elements? I have reviewed previous threads such as this one, but found outdated and non-working solutions. Additionally, I came across the jquery box-animation plugin, which is limit ...

Issues with PHP Form Email DeliveryIt seems that there is a

I currently have a ready-made form that is constructed using html, css, jquery, and ajax. It's basically copied and pasted onto my contact page for now. This is the code I include before the html tag on the contact.php page. <?php session_name(" ...

Having trouble styling my Aside element correctly

I'm having trouble aligning my aside element on the page. It's not lining up properly with the rest of the content and is overlapping into the header area. How can I resolve this issue? Here's a snapshot of what it currently looks like: ht ...

Utilize the Bootstrap grid layout to create space for empty columns on the left-hand

Is it possible to use CSS to ensure that empty columns in a predefined bootstrap grid are on the left rather than the right, without manually adding empty entries into the left hand columns? For example, if the column count is 4 (col-xs-3) and there are 5 ...

Tips for setting a specific height for an HTML table

For some reason, I can't seem to control the height of this table. I've set it to a maximum of 20px but all the rows are still showing. Here is the CSS: table { border:1px solid black; overflow:hidden; height:20px; max-height:20 ...

Use a CSS media query to elevate the third inline-block div above the initial two divs without utilizing Bootstrap

I am trying to style three colored HTML div tags displayed inline-block by using CSS only. The challenge I am facing is getting the blue div to appear on top of the red and green divs. My goal is to achieve a layout where the blue div overlaps the other tw ...