Xpath 选择器在迭代选择器列表时不会筛选出类



我正在抓取这个网站:https://www.oddsportal.com/darts/europe/european-championship/results/

这个网站使用 JavaScript 来渲染表数据,因此我在 docker 容器中使用了 scrapy-splash 插件。

我想在迭代选择器列表"tableRows"时过滤掉所有带有"黑暗中心"类的行。但是,在迭代时,xpath选择器似乎在每次迭代时查询整个选择器列表,而不是每个项目

tableRows = response.xpath('//table[contains(@id, "tournamentTable")]/tbody/tr')
    for row in tableRows:
        print(row)
        if row.xpath('//*[contains(@class, "dark center")]') is not None:
            print(True)

我的输出:

<Selector xpath='//table[contains(@id, "tournamentTable")]/tbody/tr' data='<tr class="dark center" xtid="39903"><th'>
True
<Selector xpath='//table[contains(@id, "tournamentTable")]/tbody/tr' data='<tr class="center nob-border"><th class='>
True

为什么类"中心nob-border"返回True?

你有一点错误的XPath。看看这个答案。您在第二个 XPath 表达式中错过了点。总之:

# Search document root for mentioned node.
row.xpath('//*[contains(@class, "dark center")]')
# In fact it's the same as
response.xpath('//*[contains(@class, "dark center")]')
# Search element root for mentioned node(what you're really need) is
row.xpath('./*[contains(@class, "dark center")]')
# or .//*[contains(@class, "dark center")] possibly, depending on DOM structure

这里的大更新..啊哈哈...事实上,我真的很愚蠢。井。。。实际上,您的代码中有两个错误。第一个是我提到的 Xpath 表达式。第二个是比较运算符。

row.xpath('any XPath here') is not None

将始终返回 True。由于函数返回类型是一个列表,因此它可以为空,但永远不能为 NoneType。就这样过去了。我还改进了 Xpath 选择器...最后,您需要的完全准确的代码是:

tableRows = response.xpath('//table[contains(@id, "tournamentTable")]/tbody/tr')
for row in tableRows:
    print(row)
    if row.xpath('./self::tr[contains(@class, "dark center")]'):
        print(True)

这里的主要问题是下载的页面中没有dark center。这些类是在页面加载后由一些javascript代码创建的。如果你在View Page Source中搜索它们,你找不到它们。

但是,所需的数据位于另一个 URL 中。像这样:https://www.oddsportal.com/ajax-sport-country-tournament-archive/14/rwHQ6U5F/X0/1/0/1/?_=1563816992686

$ curl -s https://www.oddsportal.com/ajax-sport-country-tournament-archive/14/rwHQ6U5F/X0/1/0/1/?_=1563816992686 | cut -c -500
-|-{"s":1,"d":{"html":"<table class=" table-main" id="tournamentTable
"><colgroup><col width="50" /><col width="*" /><col width="50" 
/><col width="50" /><col width="50" /><col width="50" /><col width="50" /></colgroup><tbody><tr class="dark center" xtid="39903"
><th class="first2 tl" colspan="7"><a class="bfl sicona s14" href="
/darts/">Darts</a><span class="bflp">u00bb</span><a class="bfl"
href="/darts/europe/"><span class="ficon f-6">&nbsp;</

您必须了解如何正确组装该 URL。我只知道最后一个参数是时间戳。祝你好运。

如果你想要所有具有类"暗中心"的元素,你可以使用:

//tr[@class="dark center"]

为什么类"中心nob-border"返回True?

结果为 true,因为您正在搜索所有 tr 标签而未指定类或 id,并且包含不适用于多个关键字。它在它们之间使用 and 或语句

参考:在不同元素上具有多个包含的 XPath

符合

您要求的正确 xpath 是//table[contains(@id, "tournamentTable")]//tr[@class='dark center']

使用此 xpath,您将在具有 id tournamentTable 的表中搜索,并使用类dard center的行进行搜索

希望对:)有所帮助

尝试获取它们:

tableRows = response.xpath('//table[contains(@id, "tournamentTable")]/tbody/tr').getall()
    for row in tableRows:
        print(row)
        if row.xpath('//*[contains(@class, "dark center")]').get() is not None:
            print(True)

最新更新