简体   繁体   English

在带有 Scrapy 的 Xpath 中使用跟随兄弟

[英]Using Following-sibling in Xpath with Scrapy

I am trying to scrape the year from the html below ( https://www.espncricinfo.com/series/indian-premier-league-2022-1298423/punjab-kings-vs-delhi-capitals-64th-match-1304110/full-scorecard ).我正在尝试从下面的 html 中抓取年份( https://www.espncricinfo.com/series/indian-premier-league-2022-1298423/punjab-kings-vs-delhi-capitals-64th-match-1304110/满分卡)。 Due to the way the site is coded I have to first identify the table cell that contains the word "Season" then get the year (2022 in this example).由于网站的编码方式,我必须首先识别包含“季节”一词的表格单元格,然后获取年份(本例中为 2022 年)。

I thought this would get it but it doesn't.我以为这会得到它,但事实并非如此。 There are no errors, just no results.没有错误,只是没有结果。 I've not used the following-sibling approach before so I'd be grateful if someone could point out where I've messed up.我以前没有使用过following-sibling方法,所以如果有人能指出我搞砸的地方,我将不胜感激。

l.add_xpath(
            'Season',
            "//td[contains(text(),'Season')]/following-sibling::td[1]/a/text()")

html: html:

<tr class="ds-border-b ds-border-line">
    <td class="ds-min-w-max ds-border-r ds-border-line">
        <span class="ds-text-tight-s ds-font-medium">Season</span>
    </td>
    <td class="ds-min-w-max">
        <span class="ds-inline-flex ds-items-center ds-leading-none">
            <a href="https://www.espncricinfo.com/ci/engine/series/index.html?season2022" class="ds-text-ui-typo ds-underline ds-underline-offset-4 ds-decoration-ui-stroke hover:ds-text-ui-typo-primary hover:ds-decoration-ui-stroke-primary ds-block">
                <span class="ds-text-tight-s ds-font-medium">2022</span>
            </a>
        </span>
    </td>
</tr>

尝试:

//span[contains(text(),"Season")]/../following-sibling::td/span/a/span/text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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