• Number Theory - HCF LCM

    <...">

    How can I only get the value 9 from this input using xpath?

    I have HTML as shown below

    <ol Class="z1">
            <li><h3>Number Theory - HCF LCM</h3>
                <p lang="title">How many pairs of integers (x, y) exist such that the product of x, y and HCF (x, y) = 1080?</p>
                <ol class="xyz">
                    <li>8</li>
                    <li>7</li>
                    <li>9</li>
                    <li>12</li>
                </ol>
            <ul class="exp"><li class="grey fleft"><span class="qlabs_tooltip_bottom qlabs_tooltip_style_33" style="cursor:pointer;"><span><strong>Correct Answer</strong>Choice (C).</br>9</span> Correct answer</span></li><li class="primary fleft"><a href="hcf-lcm_1.shtml">Explanatory Answer</a></li><li class="grey1 fleft">HCF LCM</li><li class="red1 flrt">Hard</li>
            </ul>
            </li>
    </ol>
    
          

    I'm interested in grabbing the value 9 under the correct answer from ul, whose class is exp that follows br

    I wrote an existing Xpath request that gets everything but doesn't do the job ".//ul [@ class =" exp "] / li / span / span / text () '"

    Any help is much appreciated?

    Trying to run this xpath expression while scrapy

    class BrickSetSpider(scrapy.Spider):
        name = "cat_spider"
        start_urls = ['http://iim-cat-questions-answers.2iim.com/quant/number-system/hcf-lcm/']
    
        def parse(self, response):
            CLASS_SELECTOR = '//ol[@class="z1"]/li'
            problems = []
            for lis in response.xpath(CLASS_SELECTOR):
                question = lis.xpath('.//p[@lang="title"]/text()').extract_first().strip()
                choices = lis.xpath('.//ol[@class="xyz"]/li/text()').extract()
                ANSWER_SELECTOR = './/ul[@class="exp"]/li/span/span/text()[not(contains(.,"Choice"))]'
                correct_answer = lis.xpath(ANSWER_SELECTOR).extract_first()
                explanation = lis.xpath('.//ul[@class="exp"]/li[2]/a/@href').extract_first().strip()
                difficulty = lis.xpath('.//ul[@class="exp"]/li[last()]/text()').extract_first().strip()
                p = Problem(question,choices, correct_answer, explanation, difficulty)
                print(question, choices, correct_answer)
    
          

    +3


    source to share


    3 answers


    Try below expressions and let me know if this is not what you want:



    //ul[@class="exp"]//strong[.="Correct answer"]/following::text()[2]
    
          

    +3


    source


    Use the following xpath to get the required text



    .//ul[@class="exp"]/li/span/span/text()[not(contains(.,'Choice'))]
    
          

    +3


    source


    response.xpath('//ol[@class="xyz"]/li[3]/text()').extract_first()
    
          

    UPDATE

    check = response.xpath('//ol[class="z1"]/li/ul/li/span/strong/text()').extract_first()
    if "Correct answer" in check :
        correct_answer = response.xpath('//ol[class="z1"/li/ol/li[3]/text()').extract_first()
    
          

    +1


    source







    All Articles