【问题标题】:Xpath selecting html with multiple spaces and newlineXpath选择带有多个空格和换行符的html
【发布时间】:2015-08-05 17:03:37
【问题描述】:

我正在尝试选择一个带有 class 属性的 div,该属性包含多个空格和换行符。下面是一个片段。我想选择所有带有test-onetopit 的div 的样子:

<div class="test-one
                    topit
        ">


        <div class='test-one a'>1
        </div>
        <div class='topit'>2
        </div>
</div>

<div class="test-one
                    topit
        ">


        <div class='test-one a'>1
        </div>
        <div class='topit'>2
        </div>
</div>

这是我尝试过的:

"//div[contains(concat(' ', normalize-space(@class), ' '), ' topranks ') and contains(concat(' ', normalize-space(@class), ' ), ' list-node ')]"

//*[contains(concat(' ', normalize-space(@class), ' '), ' atag ')]

我尝试改进的来源:

XPath - How to select by @text that contains new line

How can I match on an attribute that contains a certain string?

【问题讨论】:

  • 您提供的 XPath 表达式有效://div[contains(concat(' ', normalize-space(@class), ' '), ' topit ') and contains(concat(' ', normalize-space(@class), ' '), ' test-one ')](当然,您提供的表达式是 topranks 而不是 topitlist-node 而不是 test-one,但我猜你在测试时改变了它们——如果你没有改变,那就去吧)。
  • @unutbu 和 @acdcjunior 谢谢,它似乎可以工作,但在实际站点上却没有。原始 css 类 名称是 list-nodetopranks。这是链接[链接]made-in-china.com/…
  • 吉德,你的问题是什么?可以推断出一个问题,例如“我尝试过的方法不起作用。我怎样才能让它起作用?”如果这是您的问题,请告诉我们您尝试显示的 XPath 表达式时实际发生了什么。有错误吗?它没有选择任何东西吗?东西太多?做错事了?你怎么知道结果是什么?
  • @JideKoso 我坚持。您提供的 XPath 有效。我刚刚在您提供的网站上进行了尝试,并且可以正常工作。好吧,我不得不添加一个缺失的报价,但在那之后,它起作用了。试试看://div[contains(concat(' ', normalize-space(@class), ' '), ' topranks ') and contains(concat(' ', normalize-space(@class), ' '), ' list-node ')] -- 请注意我关闭了 ' &lt;QUOTE HERE&gt;), ' list-node ')] 中的引用,因为你没有它。

标签: html xml xpath


【解决方案1】:

cssselect

cssselect.GenericTranslator().css_to_xpath('div.test-one.topit')
# "descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' test-one ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' topit '))]"
tree = lxml.html.parse('http://www.made-in-china.com/companysearch.do?xcase=hunt&order=0&style=b&page=1&word=bag&size=30&sizeHasChanged=0&memberLevel=blank&sgsMembershipFlag=&comProvince=nolimit&comCity=&cateCode=&comBusinessType=blank&numEmployees=&annualRevenue=&code=0&managementCertification=').getroot()

tree.cssselect('div.list-node.topranks')
# [<Element div at 0x7f62e732dd18>, <Element div at 0x7f62e72d1f48>, <Element div at 0x7f62e72eb188>, <Element div at 0x7f62e72eb0e8>, <Element div at 0x7f62e72eb138>, <Element div at 0x7f62e72eb1d8>, <Element div at 0x7f62e72eb228>, <Element div at 0x7f62e72eb278>, <Element div at 0x7f62e72eb2c8>, <Element div at 0x7f62e72eb318>]

【讨论】:

  • 这是解决问题的好方法(如果工具链中有Python的位置),但它确实只是表明OP的XPath表达式是正确的,并没有解释为什么它不工作。换句话说,如果 OP 的 XPath 表达式对他不起作用,为什么这个解决方案会做得更好?
  • @LarsH 这里使用的表达方式不同。也许它真的会起作用。此外,OP 在关于 Python 的聊天中链接到这个问题,并声明他们使用 lxml。
  • 我看到的唯一区别是对@class and 的测试,这对于任何满足谓词其余部分的 div 来说都是已知的,因此不会影响他的案子。 descendant-or-self::// 的扩展。还有其他区别吗?除了不改变意思的括号?
猜你喜欢
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多