samedi 13 mars 2021

Como colocar uma condição "if" python scraping?

Can anyone help me with the syntax of this code? I need to put an "if" condition. How to add this condition?

vlr_atual can have two xpath, so:

if product.xpath (".//span[@id='priceblock_ourprice']/text()").get() is null then product.xpath (".//span[@id='priceblock_saleprice']/text()").get()

import scrapy
import datetime

class ProductsSpider(scrapy.Spider):
    name = 'products_details'
    allowed_domains = ['www.amazon.com.br']
    start_urls = ['https://www.amazon.com.br/Monitor-LG-19-5-LED-Inclina%C3%A7%C3%A3o/dp/B084TKF88Q/ref=sr_1_1?dchild=1&qid=1615635299&s=computers&sr=1-1']

    def parse(self, response):

        for produto in response.xpath("//div[@id='dp']"):            
            
            yield{
                'data' : datetime.datetime.now().strftime("%Y%m%d"),
                'nome': produto.xpath("normalize-space(.//span[@id='productTitle']/text())").get(), 
                'vlr_atual': produto.xpath(".//span[@id='priceblock_ourprice']/text()").get(),
                'estoque': produto.xpath("normalize-space(.//select[@name='quantity']/option[last()]/text())").get(),
                'ean': produto.xpath("normalize-space(.//table[@id='productDetails_techSpec_section_1']//tr[last()]/td/text())").get(),
            }

Aucun commentaire:

Enregistrer un commentaire