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

标签 xpath scrapy

我有一些 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,该 ul 的类是 exp,紧随 br

我编写了一个现有的 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, choices, correct_answer)

最佳答案

尝试below expression如果这不是您需要的,请告诉我:

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

关于xpath - 如何使用 xpath 从此输入中仅获取值 9?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934875/

相关文章:

html - 使用保存的 html 页面用 scrapy 抓取

php - 使用 Xpath 解析网站中的 html

php - Symfony2 Crawler - 将 UTF-8 与 XPATH 结合使用

c# - 如何在 C# 中使用 XPath 选择类名中包含字符串的节点?

python - scrapy xpath 为什么这个页面是空的,但我可以在 chrome f12 工具中看到它?

python - 将scrapy结果传递到mysql数据库

python - 我的 xpath 表达式有什么问题?

xpath - 使用xpath/scrapy选择id属性

Scrapy 头痛 - 尝试调试。没有错误,但代码不起作用

python - Scrapy 按条件停止分页?