Closed ilyi1116 closed 8 years ago
Hi @ilyi1116
非常感謝你的issue。
我嘗試了以下代碼
let jiAppleSupportDoc = Ji(htmlURL: NSURL(string: "http://www.apple.com/support")!)
let pNodes = jiAppleSupportDoc?.xPath("//p")
pNodes?.forEach { p in
debugPrint("p: \(p.content)")
}
console里打印出來的結果是:
"p: Optional(\"We’re here to help.\")"
"p: Optional(\"\\n \\t Use up and down arrows to preview suggested search results or use enter key to go to results page\\n \\t \")"
"p: Optional(\"These popular topics will help you learn more about your Apple products.\")"
"p: Optional(\"Browse\\n\\t\\t\")"
"p: Optional(\"Browse\\n\\t\\t\")"
"p: Optional(\"Browse\\n\\t\\t\")"
"p: Optional(\"Find and share solutions with Apple users around the world.\")"
"p: Optional(\"Find out about your service coverage or the progress of a repair.\")"
"p: Optional(\"Review your eligibility for support and service.\")"
"p: Optional(\"View the status of hardware service you already have in progress.\")"
"p: Optional(\"Get support by phone, chat, or email, set up a repair, or make a Genius Bar appointment.\")"
"p: Optional(\"We’re here to provide tips, tricks, and helpful information when you need it most. And if you have a question, we’re here to help with that too.\")"
"p: Optional(\"Sign up to learn the basics or take your skills to the next level.\")"
//p
應該是沒有問題的。請問您可以粘貼出更多的code嗎?沒有context,我也不知道你的具體問題出在哪裡
謝謝。
不好意思,可能我描述的不太清楚,我想擷取的是每一列的文字+圖片... 我現在的想法是先解出第一個 'p', 之後再解出每一個 'figure', 再從每一個 'figure' 去解出文字,圖片,這邊我就卡住了@@" 不知該如何下手..
你可以試試//article/p[1]
, 這樣可以取到第一個p
,然後用p.nextSibling
來獲取這個p的下一個節點,也就是figure,有了這個figure,就可以從中取出其中的圖片。
可以多次調用p.nextSibling
,直到所有的p和figure都被檢查過。
比如可以這麼寫:
let firstP = jiDoc?.xPath("//article/p[1]")?.first // You got the first `p`
var nextNode = firstP?.nextSibling
while nextNode != nil {
// Check nextNode is `p` or `figure`, do things you want
// ...
nextNode = nextNode?.nextSibling
}
方法還有很多種,你也可以先選取article
節點,然後在它的children
中篩選你需要的信息。
你好,我在使用 Ji 時,遇到了點問題.. 我的 HTML CODE 如下:
我想去Parse 每一個 'p', 'figure.img', 'figure.figcaption' .. 等 試了好多方式,好像都只能單獨取一個 tag 而已..@@?
不知是否我的寫法有誤: