首页 \ 问答 \ XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)

XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)

我想知道你是否可以帮助我。 我正在尝试使用TFHpple来使用Swift 1.2解析HTML数据,但是我在解决XPath表达式方面遇到了问题。 我收到以下错误:

可选( https://www.rac.co.uk/buying-a-car/car-passport/report/buyer/purchase/?BuyerVrm=yg06dxt

XPath错误:表达式无效
// TD [@类= 'CarMiniProfile-的tableHeader'] / TD

XPath错误:表达式无效// td [@ class ='CarMiniProfile-TableHeader'] / td

2016-03-01 16:26:23.645 CarCamera [1974:63769]无法评估XPath。 空节点!!

我试图使用的XPath代码是

var XPathString = "//td[@class=‘CarMiniProfile-TableHeader’]/td"

我想要获得的信息是所输入汽车的makemodel

<div>
  <table class="CarMiniProfile-table">
    <tbody>
      <tr class="CarMiniProfile-tableFirstRow">
        <td class="CarMiniProfile-tableHeader">
          Make
        </td>
        <td>
          FIAT
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Model
        </td>
        <td>
          PUNTO SPORTING M-JET
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Colour
        </td>
        <td>
           BLUE
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Year
        </td>
        <td>
          2006
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Engine Size
        </td>
        <td>
          1910 cc
        </td>
      </tr>
    </tbody>
  </table>
</div>

SWIFT代码:

var newURL = NSURL(string: "https://www.rac.co.uk/buying-a-car/car-passport/report/buyer/purchase/?BuyerVrm=\(numberplate)")
var htmlData: NSData = NSData(contentsOfURL: newURL!)!
var Parser = TFHpple(HTMLData: htmlData)
var XPathString = "//td[@class=‘CarMiniProfile-TableHeader’]/td"
var nodes = Parser.searchWithXPathQuery(XPathString) as NSArray

        if(nodes.count == 0 ){
            println("empty nodes!!")
        }else{
            for element in nodes
            {
                println(element.content)
            }
        }

I am wondering if you could help me out. I am trying to use TFHpple to parse HTML data using Swift 1.2, but I am having issues working out the XPath expression. I am getting the following error:

Optional(https://www.rac.co.uk/buying-a-car/car-passport/report/buyer/purchase/?BuyerVrm=yg06dxt)

XPath error : Invalid expression
//td[@class=‘CarMiniProfile-TableHeader’]/td

XPath error : Invalid expression //td[@class=‘CarMiniProfile-TableHeader’]/td

2016-03-01 16:26:23.645 CarCamera[1974:63769] Unable to evaluate XPath. empty nodes!!

The XPath code I am trying to use is

var XPathString = "//td[@class=‘CarMiniProfile-TableHeader’]/td"

The information I am trying to get is the make and model of the car entered.

<div>
  <table class="CarMiniProfile-table">
    <tbody>
      <tr class="CarMiniProfile-tableFirstRow">
        <td class="CarMiniProfile-tableHeader">
          Make
        </td>
        <td>
          FIAT
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Model
        </td>
        <td>
          PUNTO SPORTING M-JET
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Colour
        </td>
        <td>
           BLUE
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Year
        </td>
        <td>
          2006
        </td>
      </tr>
      <tr>
        <td class="CarMiniProfile-tableHeader">
          Engine Size
        </td>
        <td>
          1910 cc
        </td>
      </tr>
    </tbody>
  </table>
</div>

Swift code:

var newURL = NSURL(string: "https://www.rac.co.uk/buying-a-car/car-passport/report/buyer/purchase/?BuyerVrm=\(numberplate)")
var htmlData: NSData = NSData(contentsOfURL: newURL!)!
var Parser = TFHpple(HTMLData: htmlData)
var XPathString = "//td[@class=‘CarMiniProfile-TableHeader’]/td"
var nodes = Parser.searchWithXPathQuery(XPathString) as NSArray

        if(nodes.count == 0 ){
            println("empty nodes!!")
        }else{
            for element in nodes
            {
                println(element.content)
            }
        }

原文:https://stackoverflow.com/questions/35728317
更新时间:2020-11-03 19:58

最满意答案

第一:在您的样本中,您有:

 <td class="CarMiniProfile-tableHeader">

但是你在询问

 //td[@class=‘CarMiniProfile-TableHeader’] 

(注意TableHeader中的大写'T')。 xpath区分大小写。

第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。

有很多方法可以在这里获得制作和模型

  • 选项1:向上一级并从TR // tr / td [2]中选择将返回一个节点列表(tr的每秒td-child)
  • 选项2:使用兄弟轴:// td / following-sibling :: td [1]

First: in your sample you have:

 <td class="CarMiniProfile-tableHeader">

but you were querying for

 //td[@class=‘CarMiniProfile-TableHeader’] 

(notice the capital 'T' in TableHeader). xpath is case sensitive.

Second: by querying for //td[@class=‘CarMiniProfile-TableHeader’]/td you imply that you have a 'td' element inside the outer td, whereas they are siblings.

There are a number of ways to get the make and model here a couple

  • option 1: go one level up and select from the TR //tr/td[2] will return you a list of nodes (every second td-child of tr)
  • option 2: use the sibling axis: //td/following-sibling::td[1]
2016-03-02

相关文章

更多

最新问答

更多
  • 如何保护Solr只允许SELECT请求给用户并禁止其他任何东西?(How to secure Solr to allow SELECT request to only users and disallow anything else?)
  • XPath表达式无效/错误TFHpple SWIFT 1.2(XPath Expression not working/incorrect TFHpple SWIFT 1.2)
  • css3 3D变换不能平滑地制作动画(css3 3D transform doesn't animate smoothly)
  • 运行时错误'91'和Outlook.Application = <对象变量或没有设置块变量>?(Run-time error '91' & Outlook.Application = Object variable or With block variable not set?)
  • 慢的webservice问题(Slow webservice problem)
  • textview的不正确对齐方式(Improper alignment of a textview)
  • 在第一步“Hello World”中出现Java错误(Cannot run “Hello World” program in Eclipse)
  • 为什么十六进制地址是14个字符?(why the hex address is 14 character?)
  • 如何在Python中的不同类中使用变量?(How to use variable in different classes in Python?)
  • asp:GridView HYPERLINKFIELD - datanavigateurlformatstring中的asp代码(asp:GridView HYPERLINKFIELD - asp code inside datanavigateurlformatstring)
  • 关于adaboost算法(About adaboost algorithm)
  • 在Matlab上内置图像(Built in Images on Matlab)
  • java swing:输入键事件时焦点丢失(java swing: Focus lost on enter key event)
  • C#中的通用约束,T是相同的TSomethingElse,对吧?(Generic constraints in C#, T is the same TSomethingElse, right?)
  • 从mybatis中的光标获取数据(Fetching data from cursor in mybatis)
  • 在运行时从XML构建对象的最佳方法(Best way for building objects out of XMLs at runtime)
  • 在整个窗口中拉伸sf :: Sprite(Stretch sf::Sprite across entire window)
  • Selenium Node API Web驱动程序等待超时处理程序(Selenium Node API web driver wait timeout handler)
  • 函数使用并且是map的一部分(循环依赖?)(Function uses and is part of map (circular dependency?))
  • 是否可以在C ++ 14中使用可选模板参数创建类型元组?(Is possible to make a tuple of types with optional template parameters in C++14?)
  • PHP从窗帘后面打印/ f(PHP prints /f from behind the curtains)
  • JFrame的contentPane的LayoutManager(LayoutManager of JFrame's contentPane)
  • 用于Instagram Feed的图像滚动(Image Roll Overs for Instagram Feed)
  • 如何显示拉伸字体(双倍宽度/高度)?(How to display stretched font (double width/height)?)
  • 文件操作API在fileapi.h和stdio.h中的WP8差异?(WP8 differences between file manipulation APIs in fileapi.h and stdio.h?)
  • 保存失败后Rails没有回滚事务()(Rails not rolling back transaction after failed save())
  • jqgrid中的分页问题与数组数据(Pagination problem in jqgrid with array data)
  • 重定向时,通过其他页面上的URL调用javascript函数(Call javascript function through url on otherpage while Redirecting)
  • 如何使用InvokeCommandAction调用我的方法并传入参数?(How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?)
  • Jquerymobile按钮仅显示页面的第一个外观(Jquerymobile buttons are shown only first apperance of the page)