Xpath returns null

2024/11/15 8:35:05

I need to scrape the price of this page: https://www.asos.com/monki/monki-lisa-cropped-vest-top-with-ruched-side-in-black/prd/23590636?colourwayid=60495910&cid=2623

However it is always returning null:

My code:

'price' :response.xpath('//*[contains(@class, "current-price")]').get()

image

Can someone help please?

Thanks!

code

When Extracted using XHR: enter image description here

How to retrieve price?

Answer

Your problem is not the xpath, it's that the price is being retrieved with XHR.

If you use scrapy sheel and type view(response) you can see that the price is not being generated: enter image description here

Look at the source of the original webpage and search for the price: enter image description here

Then use this url the scrape the price:

    def parse(self, response):import reprice_url = 'https://www.asos.com' + re.search(r'window.asos.pdp.config.stockPriceApiUrl = \'(.+)\'', response.text).group(1)yield scrapy.Request(url=price_url,method='GET',callback=self.parse_price,headers=self.headers)def parse_price(self, response):import jsonjsonresponse = json.loads(response.text).............................................

I couldn't get around 403 error with the headers I provided, but maybe you'll have more luck.

Edit:

In order to get the price from the json file there's actually no need for json.loads

    def parse_price(self, response):jsonresponse = response.json()[0]price = jsonresponse['productPrice']['current']['text']# You can also use jsonresponse.get() if you preferprint(price)

Output:

£10.00
https://en.xdnf.cn/q/119068.html

Related Q&A

I am getting an Index error as list out of range. I have to scan through many lines

import nltk import random from nltk.tokenize import sent_tokenize, word_tokenizefile = open("sms.txt", "r") for line in file:#print linea=word_tokenize(line)if a[5] == SBI and a[6]=…

How can i append two classes in JQuery

I have implemented a chat box message (live chat) using django and now i want to add css, but i have problem on how to append multiple classes on messege sent. For example, i want to show other user me…

After installing PyBluez on Windows8.1 I get DLL %1 not valid win32 app

I have installed PyBluez-0.22.win32.exe on a 64bit machine with Python 2.7 (they didnt have a 64bit version). Then I get the following error: ImportError:DLL load failed:%1 is not valid Win32 applicati…

Flask Apache on AWS EC2 - Read/Write Failing

So Ive been stumped by this problem for a day now. Im relatively new to AWS EC2 so have been experimenting with Python Flask apps on it.I have an Ubuntu instance, and can get a flask app to run fine on…

AttributeError: unicode object has no attribute pop

I have this piece of python code in gae.def post(self):cases=self.request.get(cases)while cases:logging.info("cases: %s " % cases)case=cases.pop()Which produces this log.INFO 2012-09-19 2…

How to set platform when using pip download command

I want to download some pacakges(tensorflow, keras, imbalanced, xgboost, lightgbm, catboost) for centos 7.4 and python 3.7 on mac.How should i set platform name and ant other settings?I used below com…

How to split qr codes in separate images?

I have hundreds of pictures with qr codes (sometimes there are 0, 1, 2 or more qr codes on one page...but they are always in one line). I want to decode the qr codes from left to right. My idea is to s…

Python methods from csv

I am working on an assignment where I create "instances" of cities using rows in a .csv, then use these instances in methods to calculate distance and population change. Creating the instance…

Convert string numpy.ndarray to float numpy.ndarray

I have one problem. How can I convert:import numpy as npa = np.array([[0.1 0.2 0.3], [0.3 0.4 0.5], [0.5 0.6 0.7]])To:b = np.array([[0.1,0.2,0.3], [0.3,0.4,0.5], [0.5,0.6,0.7]])

function at the end of code- why do i have to place it?

Im just starting my adventure with Python. Unfortunately, I cant figure out why, at the end of my code, I have to add myfunc(). Without it my code doesnt display. How is it if I use more than one def…