简体   繁体   中英

Scrapy unable to get xpath

I got this code in this website NTP

        <h5>Soundbooster</h5> <br><br>
      <p class="details">
        <b>Filtro attuale</b>
      </p>
      <blockquote>
        <p>
          <b>Catalogo:</b> 
            Aliant</br>
          <b>Marca e Modello:</b> 
            Mazda - 3 </br>
          <b>Versione:</b> 
            (3th gen) 2013-now (Petrol)
        </p>
      </blockquote>

And I am trying to extract the element "Mazda - 3" and I am unable to get it, it return blank. In the code, the "Mazda - 3" part is in brand value. I get the name and the version value.

This is how I implemented:

    for ntp in response.css('div.content-1col-nobox'):

        name = ntp.xpath('normalize-space(//h5/text())').extract_first()
        brand = ntp.xpath('normalize-space(//blockquote/p//text()[4])').extract_first()
        version = ntp.xpath('normalize-space(//div/blockquote[1]/p//text()[6])').extract_first()
        result = ("{}  {} - {}".format(name, brand, version))

This post is related to this one, it work there, but I realized that I get only part of the data. See here: Scrapy add.xpath or join xpath

Can anybody help me please.

Thank you in advance.

我不确定您的代码中有什么ntp ,但这应该可以工作:

brand = ntp.xpath('.//b[.="Marca e Modello:"]/following-sibling::text()[1]').extract_first()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM