加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何使用xpath从此输入中仅获取值9?

发布时间:2020-12-20 12:00:09 所属栏目:Python 来源:网络整理
导读:我有一些 HTML,如下所示 ol Class="z1" lih3Number 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" li8/li li7/li li9/li li12/li /ol ul class="exp"li
我有一些 HTML,如下所示

<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>

我有兴趣从ul的正确答案中获取值9,其类是exp后面的exp

我编写了一个现有的Xpath查询,它可以获得所有内容但是并没有完成这项工作“.// ul [@ class =”exp“] / li / span / span / text()’”

任何帮助都非常感谢?

试图在scrapy上运行这个xpath表达式

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,correct_answer)

解决方法

试试 below expression并告诉我这是不是你需要的:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读