tid-kijyun / kanna Goto Github PK
View Code? Open in Web Editor NEWKanna(鉋) is an XML/HTML parser for Swift.
License: MIT License
Kanna(鉋) is an XML/HTML parser for Swift.
License: MIT License
I followed your instructions, but still
import Kanna gives an error ( No such module 'Kanna' )
let inputdata = NSData(url:"http://www.obaiwan.com/wan-index-1.html")
let html:String? = NSString(data: inputData, encoding: NSUTF8StringEncoding) as? String
if let nilhtml = html {
var err : NSError?
var parser = HTMLParser(html: nilhtml, error: &err)
if err != nil {
println(err)
exit(1)
}
var bodyNode = parser.body
if let tableNodes = bodyNode?.findChildTag("div") {
for node in tableNodes {
println(node.contents)
//println(node.getAttributeNamed("href"))
}
}
}
can not find any tag?
can not find body?
trying to parse this page : http://gbmontreal.ca/about-us/schedule/
Getting the a tag's inner content works fine but if I try table, I get this error :
648: HTML parser error : Tag nav invalid ...
why is the a tag working and not the table one ?
Hi. I am trying to get Kanna to run in a playground and I am getting this error in the console:
Playground execution failed: <module-includes>:1:9: note: in file included from <module-includes>:1:
#import "/Users/cj/iOS/TestProject/Pods/Kanna/Modules/libxml2-kanna.h"
^
/Users/cj/iOS/TestProject/Pods/Kanna/Modules/libxml2-kanna.h:1:9: note: in file included from /Users/cameronjackson/iOS/MangaLoop/Pods/Kanna/Modules/libxml2-kanna.h:1:
#import <libxml2/libxml/HTMLtree.h>
^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.2.sdk/usr/include/libxml2/libxml/HTMLtree.h:15:10: error: 'libxml/xmlversion.h' file not found
#include <libxml/xmlversion.h>
^
could not build Objective-C module 'libxml2'
I am using XCode 7.2 and iOS 9. I have installed Kanna via CocoaPods. I have searched for solutions but I haven't come across any solutions. I have another pod, Alamofire, that is not causing any issues. Any help would be appreciated.
As I know, Using _Bridging_Header.h just is necessary in Object-C project.
I tried to put the HTMLParsing.swift and HTMLNode.swift files to my Swift project but it didn't work.
:(
Thanks your nice project, help me a lot.
XD
Getting this error.
:76: element div: validity error : ID header-print already defined
How can I get it to continue past this? And no I can not remove the duplicate tag.
I am trying to do a manual import to my xcode project as in step 4 the process below:
In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field
I keep getting a build error that says "No such module 'libxml2'
I am using xcode 7.2 running on El Capitan.
Any help you can provide would be greatly appreciated.
Thanks in advance.
for this xpath:
(//a[contains(@href, 'product')])[1]/@href
at browser it return:
product/7721628;jsessionid=W0eC3Cam8rXg5hLeBODUFQ__.node2?site=sr:SEARCH:MAIN_RSLT_PG
with your library:
href="product/7721628;jsessionid=e8za0YLN1vFhtqK7t6up2A__.node4?site=sr:SEARCH:MAIN_RSLT_PG"
It would be nice to support tvOS. I tried to add a tvOS target on my own, but I have no idea how to properly link libxml. It would be really nice if i could get it working on the apple tv.
trying to parse this page : http://gbmontreal.ca/about-us/schedule/
Getting the a
tag's inner content works fine but if I try table
, I get this error :
648: HTML parser error : Tag nav invalid ...
why is the a
tag working and not the table
one ?
extension XMLNodeSet: SequenceType {
public typealias Generator = AnyGenerator
public func generate() -> Generator {
var index = 0
return AnyGenerator { // Argument passed to call that takes no arguments
if index < self.nodes.count {
let n = self.nodes[index]
index += 1
return n
}
return nil //Type of expression is ambiguous without more context
}
}
}
So I was using Kanna to parse some HTML on my iOS app.
Everything works well on Debug mode no problem.
When I tested the app on TestFlight it just crashed. Took me a while to figure out the problem.
It happens only in Release mode so I went ahead and changed the scheme to Release mode.
Firing up the app it crashes as the TestFlight version, as "expected".
The problem resides in the class libxmlHTMLNode
in the following method:
private func libxmlGetNodeContent(nodePtr: xmlNodePtr) -> String? {
let content = xmlNodeGetContent(nodePtr)
if let result = String.fromCString(UnsafePointer(content)) {
content.dealloc(1)
return result
}
content.dealloc(1)
return nil
}
The line let content = xmlNodeGetContent(nodePtr)
has a EXC_BAD_ACCESS and when I try to print out the nodePtr I get the following message:
Kanna was compiled with optimization - stepping may behave oddly; variables may not be available.
So I went ahead and turned `Swift Compiler Optimization Level to None[-Onone] and the app runs propperly.
My app is a Swift app using Kanna installed via CocoaPods.
Not sure what I can do.
Anybody had this problem before?
When I add github "tid-kijyun/Kanna" ~> 1.0.0
and run carthage update
.
During === BUILD TARGET Kanna_watchOS OF PROJECT Kanna WITH CONFIGURATION Release ===
I got error: no such module 'libxml2' import libxml2
.
I may well have been in error, but calling XMLElement.css with just the function "::after" appears to cause a loop / hang. I am pretty new to CSS and Kanna, but was trying to find key value pairs in the following manner
let label = detail.css("strong").first!
let value = label.css("::after")
My program / script then hung, with a stack like this:
#0 0x00007fff8c6f40a3 in malloc ()
#1 0x00007fff913a8f34 in icu::RegexCompile::compile(UText*, UParseError&, UErrorCode&) ()
#2 0x00007fff913a30db in icu::RegexPattern::compile(UText*, unsigned int, UParseError&, UErrorCode&) ()
#3 0x00007fff913a2cdf in uregex_open ()
#4 0x00007fff8e73db70 in -[NSRegularExpression initWithPattern:options:error:] ()
#5 0x00000001000f25a3 in @nonobjc NSRegularExpression.init(pattern : String, options : NSRegularExpressionOptions) throws -> NSRegularExpression ()
#6 0x00000001000f2440 in NSRegularExpression.__allocating_init(pattern : String, options : NSRegularExpressionOptions) throws -> NSRegularExpression ()
#7 0x00000001000eff5e in (firstMatch in _CA6A004099612D342CE85B6160D34CF2)(String) -> (str : String) -> NSTextCheckingResult? at /path/Pods/Kanna/Source/CSS.swift:83
#8 0x00000001000ecc55 in (getAttribute in _CA6A004099612D342CE85B6160D34CF2)(inout String, skip : Bool) -> String? at /path/Pods/Kanna/Source/CSS.swift:216
#9 0x00000001000e908e in static CSS.toXPath(String) -> String? at /path/Pods/Kanna/Source/CSS.swift:59
#10 0x00000001001046dd in libxmlHTMLNode.css(String, namespaces : [String : String]?) -> XMLNodeSet at /path/Pods/Kanna/Source/libxml/libxmlHTMLNode.swift:144
#11 0x0000000100104a6c in libxmlHTMLNode.css(String) -> XMLNodeSet at /path/Pods/Kanna/Source/libxml/libxmlHTMLNode.swift:155
I have other means of getting the element I want, but wasn't expecting a hang. It is likely that you need to pass a path to after, I was just hoping that I could get the next sibling for a given element.
Otherwise, I'm really enjoying the library - Thanks!
I keep getting
Use of undeclared type 'htmlDocPtr'
And similar type errors. I'm assuming there is some lib I did not link correctly, even though I followed the Usage on the README
.
for element in doc.css("p, br") {
let parent = element.getParentElement?
}
is there any way to get this?
Code Sign error: No code signing identities found: No valid signing identities (i.e. certificate and private key pair) matching the team ID “(null)” were found.CodeSign error: code signing is required for product type 'Framework' in SDK 'iOS 8.4'
I got this error when I tried to install Ji with Carthage. I am really new to iOS development and Swift.
Any ideas?
I have been able to extract data from the outer most XML tag. However, I have not been able to figure out how to extract data from sub tags. For eg.
<Person>
<Name>Bob</Name>
<Age>21</Age>
</Person>
<Person>
<Name>Alice</Name>
<Age>20</Age>
</Person>
How do i get the name and age in the example XML shown above?
Hi guys!
I currently run into trouble with this selector #content > p:not(.article-meta)
.
When I run this selector into JSFiddle, the right parts are coloured red. But Kanna returns also the p.article-meta with the same selector.
Is this expected behaviour, or am I using the libs wrong?
This is just a heads-up, but while Swift-HTML-Parser compiles and runs correctly as Swift 2.0 code in "Debug" builds, it currently crashes the Xcode 7.0b (7A120f) compiler when trying to be built under a "Release" build. This is likely to be a bug in the optimizer itself (I found a similar problem with a convenience init?() method I had written in a different file). Just an FYI for future reference...
Command failed due to signal: Abort trap: 6
...Build/Intermediates/Swift-HTML-Parser.build/Release/Swift-HTML-Parser.build/Objects-normal/x86_64/HTMLParser.o
Assertion failed: ((!FileContext || issue != Status::Malformed) && "error deserializing an individual record"), function error, file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.38.1/src/swift/include/swift/Serialization/ModuleFile.h, line 348.
0 swift 0x0000000104acee0b llvm::sys::PrintStackTrace(__sFILE*) + 43
1 swift 0x0000000104acf54b SignalHandler(int) + 379
2 libsystem_platform.dylib 0x00007fff8cb49f1a _sigtramp + 26
3 libsystem_platform.dylib 0x0000000000000048 _sigtramp + 1934319944
4 libsystem_c.dylib 0x00007fff8a796b53 abort + 129
5 libsystem_c.dylib 0x00007fff8a75ec39 basename + 0
6 swift 0x0000000102eaabb1 swift::SILDeserializer::readSILFunction(llvm::Fixnum<31u, unsigned int>, swift::SILFunction*, llvm::StringRef, bool, bool) + 3889
7 swift 0x0000000102eb7399 swift::SILDeserializer::lookupSILFunction(swift::SILFunction*) + 137
8 swift 0x0000000102f0af8b swift::SerializedSILLoader::lookupSILFunction(swift::SILFunction*) + 59
9 swift 0x0000000102cd09e5 swift::SILLinkerVisitor::process() + 581
10 swift 0x0000000102cd072e swift::SILLinkerVisitor::processFunction(swift::SILFunction*) + 286
11 swift 0x0000000102c7248b swift::SILModule::linkFunction(swift::SILFunction*, swift::SILOptions::LinkingMode, std::__1::function<void (swift::SILFunction*)>) + 299
12 swift 0x0000000102dc016b (anonymous namespace)::SILLinker::run() + 75
13 swift 0x0000000102dc79a8 swift::SILPassManager::runOneIteration() + 2072
14 swift 0x0000000102dc84bc swift::SILPassManager::run() + 1212
15 swift 0x0000000102dc4497 swift::runSILOptimizationPasses(swift::SILModule&) + 375
16 swift 0x0000000102ae883f performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 9583
17 swift 0x0000000102ae60b3 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2515
18 swift 0x0000000102ae228f main + 1983
19 libdyld.dylib 0x00007fff8fcdf5c9 start + 1
20 libdyld.dylib 0x000000000000003f start + 1882327671
Have troubles with queries that contains logical operations. They always return empty results. I saw sources you use concat and other logical operations here: https://github.com/tid-kijyun/Kanna/blob/master/Source/CSS.swift#L189 . Why my is not work?
for node in doc.xpath("concat((//a/@href)[1], (//a/@href)[2])") {
output += node.toHTML!`
output += node.text!
}
I'm currently getting a compilation error on XCode 7 beta 2 and 3.
CompileSwift normal arm64 /Users/Kyro/dev/BusTramGre/BusTramGre/HTMLParser.swift
[...]
Assertion failed: ((!FileContext || issue != Status::Malformed) && "error deserializing an individual record"), function error, file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.45/src/swift/include/swift/Serialization/ModuleFile.h, line 352.
This is probably a swift Compiler bug but it there a way to get around this ?
Edit:
BTW the problematic line seems to be :
CFStringGetCStringPtr(cfencstr,CFStringGetSystemEncoding())
pod logs:
[!] Error installing Kanna
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
After updating Xcode to the latest version, I get the following error:
"/Users/nsalerni/Documents/GitHub/GODelays-iOS/GODelays/GODelays/FeedViewController.swift:11:8: Module file was created by an older version of the compiler; rebuild 'Kanna' and try again: /Users/nsalerni/Library/Developer/Xcode/DerivedData/GODelays-fzmhfgqmmokmdbfhgeigokyaqqar/Build/Products/Debug-iphonesimulator/Kanna.framework/Modules/Kanna.swiftmodule/x86_64.swiftmodule"
AppDelegate.swift:11:1: 'UIApplicationMain' attribute cannot be used in a module that contains top-level code
I know it was released out of the beta just yesterday, but I'm having some errors when opening with Swift 1.2 and Xcode 6.3. Some errors are easily fixed with the new as! syntax but some are not. Here are the two errors that I couldn't fix:
HTMLNode.swift:
MyViewController.swift (when I try to initialise var parser = HTMLParser(html: html, encoding: encoding, error: &err))
Any ideas on what they might be or how to fix them?
Thanks,
Beno
This minimal sample was able to reproduce it:
if let doc = Kanna.HTML(html: "<html><body><div id=\"asd\">asd</div></body></html>", encoding: NSUTF8StringEncoding) {
doc.at_css("#asd")!.innerHTML
}
The error throws up deep in the stack trace on the inner return of this method:
private func libxmlGetNodeText(nodePtr: xmlNodePtr) -> String? {
if nodePtr != nil {
let type = nodePtr.memory.type
if type.rawValue == XML_TEXT_NODE.rawValue {
return libxmlGetNodeText(nodePtr) // <-- Here
}
}
return nil
}
This error occurs ~2000 recursive calls deep.
It would be helpful to explain a little bit more how this library works, actually the short documentation you provide isn't really clear
hey , i know it's a framework , but i don't know how to use them in my project for ios.
i tried to build Kanna.xcodeproj , then in build folder -> Release-iphonesimulator a framework made ( Kanna.framework ) , i added it to my project , then >> import Kanna and when i tried to run , i got a error , something was missed ... i just found this way of making frameworks from google , i don't know if i did it right .
i need Kanna :)
I'm trying to parse this site: http://old.gjk.cz/suplovani.php
The problem is it's in Czech which in Swift encoding means NSISOLatin2StringEncoding but for whatever reason with this code it doesn't work:
let url = NSURL(string: "http://old.gjk.cz/suplovani.php")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
data, response, error in
if let table = Kanna.HTML(html: data!, encoding: NSISOLatin2StringEncoding) {
print(table.title!)
}
The output is table = nil
If I use NSISOLatin1StringEncoding though, table != nil, but the output is Bakaláøi - Suplování because of the wrong encoding. Any ideas why NSISOLatin2StringEncoding doesn't work? It works when I don't use Kanna framework with print(NSString(data: data!, encoding: NSISOLatin2StringEncoding))
I am using XCode beta 7.1.
Is there a way to set the values of nodes, not just get them?
I am getting many memory leaks traced to Kanna.
When you change the sample HTML to have 2 box classes,
...... " <div class='box'>" + " <span class='dat'>" + " <strong content='2014-12-31'>2014/12/31</strong>" + " </span>" + " <h2>Hoge</h2>" + " </div>" + " <div class='box'>" + " <span class='dat'>" + " <strong content='2014-12-31'>2014/12/31</strong>" + " </span>" + " <h2>SecondHoge</h2>" + " </div>" ......
and then when you run
if let path = bodyNode?.xpath("//div[@class='box']") { for node in path { println("fancy node \(node.tagName)") println(node.xpath("//h2")?.count) } }
it returns
fancy node div
Optional(2)
fancy node div
Optional(2)
as answer, which I think is wrong and should return 1 & 1.
In a playground with the Kanna module available as a framework:
import Kanna
let test1 = Kanna.HTML(html: "<html><body>Testing Something</body></html>", encoding: NSUTF8StringEncoding)
test1?.body?.innerHTML // "Testing Something"
let test2 = Kanna.HTML(html: "<html><body>Testing<p>Something</body></html>", encoding: NSUTF8StringEncoding)
test2?.body?.innerHTML // "\n"
The second test produces unexpected result of "\n" instead of "Testing<p>Something".
`*** Building scheme "Kanna_tvOS" in Kanna.xcworkspace
** BUILD FAILED **
The following build commands failed:
CompileSwift normal arm64 /workspace/Carthage/Checkouts/Kanna/Sources/Kanna.swift
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
(2 failures)
/workspace/Carthage/Checkouts/Kanna/Sources/Kanna.swift:253:20: error: type of expression is ambiguous without more context
/workspace/Carthage/Checkouts/Kanna/Sources/Kanna.swift:247:29: error: argument passed to call that takes no arguments
A shell task (/usr/bin/xcrun xcodebuild -workspace /workspace/Carthage/Checkouts/Kanna/Kanna.xcworkspace -scheme Kanna_tvOS -configuration Release -sdk appletvos ONLY_ACTIVE_ARCH=NO BITCODE_GENERATION_MODE=bitcode CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES clean build) failed with exit code 65:
** BUILD FAILED **
The following build commands failed:
CompileSwift normal arm64 /workspace/Carthage/Checkouts/Kanna/Sources/Kanna.swift
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
(2 failures)`
Hi, i have followed the steps mentioned in the link :https://github.com/tid-kijyun/Kanna. But i keep getting the error : No such module - libxml2.
After using your files, my project looks like this -
Also i have set the Swift Compiler - Search Paths > Import Paths as "$(SRCROOT)/Bhagavad Geetha Book/Modules".
Also, I am a little unclear with - "Step : 1 Add files to your project:" and "Step : 2 Copy folder to your project:" . Hopefully i have set your files correctly.
Kindly let me know if i have missed anything. Thanks in advance.
I install this to my project via pods but can't seem to get it to work
Just had a bug report in my app that uses Kanna where text is missing from a page that's been parsed. It appears that a long line of text ending in <br><br> is missing from the innerHTML string.
if let doc = Kanna.HTML(html: data!, encoding: NSUTF8StringEncoding){
for articleParagraphs in node.css("p") {
if articleParagraphs.className == "small" {
print("StarDate: " + articleParagraphs.text!)
} else {
print("Article: " + articleParagraphs.innerHTML!)
}
}
Example can be found here
Expected result:
Last month, the eminent scientist Ishmael Palin...
Actual result:
Since then, certain sections of the galactic...
articleParagraphs.text
shows that the all of the expected text is present and it appears to be an issue with the string returned by articleParagraphs.innerHTML
Is there a way to parse "nth-child(n)"?
I have a very large table and I am trying to get data by specifying row and column numbers. I got it to work using Nokogiri in Ruby for a web app. I can't figure a way to do it in Swift.
I like testing framework using playground (use this tool), which works fine with most pods like Alamofire, but your framework need to include libxml2, this some kind of improving the complication and it indeed not work, can you help me inspect this problem
i am unable to retrieve particular div tag using doc.css , please help me with the issue
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.