honghaoz / Ji

Ji (戟) is an XML/HTML parser for Swift
MIT License
824 stars 64 forks source link

有些特定xpath不能解析 #13

Closed yudun1989 closed 8 years ago

yudun1989 commented 8 years ago
    let contentData = NSData(contentsOfURL: NSURL(string: "http://v2ex.com/t/223195")!)
    let jiDoc = Ji(htmlData: contentData!)
    let nodes = jiDoc?.xPath("//div[starts-with(@id, 'r_')]")

是想解析里面含r_ 的div,用chrome 的xpath helper 可以将里面帖子全部解析出来,但是如果用Ji来解析 话,nodes没有结果。

看了下hpple也有这个问题,是什么原因?

honghaoz commented 8 years ago

@yudun1989 Hi,多谢新的issue。我试了试,应该是可以解析的。

Code I tried:

let gitHubStatusDoc = Ji(htmlURL: NSURL(string: "https://status.github.com")!)!
if let divs = gitHubStatusDoc.xPath("//div[starts-with(@id, 'me')]") {
    print("divs: \(divs)")
} else {
    print("divs: not found")
}

Results:

divs: [<div id="message" class="good">

      All systems operational
  </div>]

注意,tag的大小写不要错了,不然也会解析错误,比如"//div[starts-with(@ID, 'me')]"就会失败

yudun1989 commented 8 years ago

感谢回复。我这边的话starts-with规则也不是所有的都不能解析,比如 http://v2ex.com/t/224077 这个里面的id规则 starts-with r_的div就是可以全部提取出来的。

看了下源码 if nodeSet == nil || nodeSet.memory.nodeNr == 0 || nodeSet.memory.nodeTab == nil { // NodeSet is nil. xmlXPathFreeObject(xPathObject) return [] } nodeNr返回的结果就是0。所以觉得也挺奇怪的。是不是libxml2的原因呢?

yudun1989 commented 8 years ago

我试了下解析github stats.也可以解析出来。 所以有可能是这个 http://v2ex.com/t/223195 页面有问题?

honghaoz commented 8 years ago

@yudun1989 我也还真不太清楚,不行你换换别的方式解析。有的页面可能前面的编码有问题会影响后面的。你试试parse下header tag里的东西,看看starts-with可以用吗?

yudun1989 commented 8 years ago

@honghaoz 之前试contains也不行。 奇怪。我再多试试别的内容。

honghaoz commented 8 years ago

@yudun1989 是不是这个页面的问题,你试着把页面的string赋值给一个变量,然后用htmlString的方式解析看看。

yudun1989 commented 8 years ago

。。。丢人了。是我这边的问题。So Soooooooooooorry. 这个页面需要signin.

honghaoz commented 8 years ago

哈哈哈,没有关系,出问题就需要讨论讨论就知道原因了嘛

— Sent from Mailbox

On Mon, Sep 28, 2015 at 12:20 AM, yudun1989 notifications@github.com wrote:

Closed #13.

Reply to this email directly or view it on GitHub: https://github.com/honghaoz/Ji/issues/13#event-420377307