Issue
I am doing my scrapy code where I have to extract data from website https://lemkus.com/collections/maylee/products/hannah-green-cord-pants?variant=43116172181756
Now Output should be like if item is out of stock only then show out_of_stock='True' along with size and otherwise dont show out_of_stock
Sample JSON output:
"skus": [
{
"currency": "ZAR",
"out_of_stock": true,
"price": 850.00,
"size": "8"
},
{
"currency": "ZAR",
"price": 850.00,
"size": "10"
},
{
"currency": "ZAR",
"out_of_stock": true,
"price": 850.00,
"size": "12"
},
{
"currency": "ZAR",
"price": 850.00,
"size": "14"
}
The HTML Cde of website is:
<fieldset class="fieldset-var unavailable-product" data-var-id="43116172181756">
<input class="js-variant-radio" name="id" type="radio" id="variant-1" value="43116172181756" data-inventory-qty="0">
<label for="variant-1">8</label>
</fieldset>
<fieldset class="fieldset-var variant-active " data-var-id="43116172214524">
<input class="js-variant-radio" name="id" type="radio" id="variant-2" value="43116172214524" data-inventory-qty="1">
<label for="variant-2">10</label>
</fieldset>
<fieldset class="fieldset-var unavailable-product" data-var-id="43116172247292">
<input class="js-variant-radio" name="id" type="radio" id="variant-3" value="43116172247292" data-inventory-qty="0">
<label for="variant-3">12</label>
</fieldset>
<fieldset class="fieldset-var " data-var-id="43116172280060">
<input class="js-variant-radio" name="id" type="radio" id="variant-4" value="43116172280060" data-inventory-qty="1">
<label for="variant-4">14</label>
</fieldset>
OK, So in the HTML Code above,see where the tag "data-inventory-qty" is equal to zero and in some fields its greater than 0 I want to do is if data-inventory-qty="0", Show out_of_stock='True' otherwise don't show out of stock
I have logic for extracting currency,price and size. I just need the out_of_stock by extracting the value of data-inventory-qty or using another attribute that might work.
A part of the code for out_of_stock I have done until now is
items=response.xpath('//div//form//div//fieldset//input[@data-inventory-qty])'.getall()
I need Like there are 4 sizes in the above code so make a list of 4 data. if data-inventory-qty is 0 with first item then append 'True' in the list otherwise append '' in the list.
So list for above HTML should be like ['','True','','True']
Solution
Something like this?
lst = response.xpath('//input[@class="js-variant-radio"]/@inventory-qty').getall()
result = [True if not int(i) else '' for i in lst]
print(result)
OUTPUT
[True, '', True, '']
Answered By - Alexander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.