【问题标题】:Simple XPath expression doesn't work in Python简单的 XPath 表达式在 Python 中不起作用
【发布时间】:2012-08-06 19:36:40
【问题描述】:

我正在尝试解析一个 Web 文档(基于表格设计),并且我有一个带有表格的 div。

<div id="component">
   <table ...>
      ....

如果我尝试:

XmlXPathSelector(response).select("//*[@id='component']")

这行得通:

[<XmlXPathSelector xpath="//*[@id='component']" data=u'<div xmlns="http://www.w3.org/1999/xhtml'>]

但如果我想列出包含的表格:

XmlXPathSelector(response).select("//*[@id='component']/table")

这将返回一个空结果。我尝试使用其他选择器,例如 "//*[@id='component']/tr" 和 "//*[@id='component']/a",但它们也不起作用。

有什么想法可能是错的吗?

【问题讨论】:

  • 请发帖html document。
  • 从您的示例中,它应该是"news",而不是"component"。
  • 是的,我知道,我写这个 HTML 作为示例,这不是问题。

标签: python xpath python-2.7 scrapy


【解决方案1】:

:P 我解决了这个问题,我使用的是 XmlPathSelector 而不是 *HTML*PathSelector

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多