After one day, I want to use hpple to easily parse HTML using XPath.
Reference is the practice here: http://lwxshow.com/ios-iphone-development-teaches-you-how-to-use-the-objective-c-parsing-html-lwxshow-com
(Related:
Http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone
Http://stackoverflow.com/questions/9746745/xpath-attributes-selection
)
It said in a very detailed: Is to reference the https://github.com/topfunky/hpple on the hpple library, combined with libxml, you can use XPath to search HTML.
For more information about XPath, see w3school's tutorial http://www.w3school.com.cn/xpath/index.asp
After the related configuration is complete, you can use it directly:
-( Void ) Viewdidload {[Super viewdidload]; nserror * Error; nsdata * Htmldata =[[Nsstring stringwithcontentsofurl: [nsurl urlwithstring: @" Http://dict.youdao.com/m/search? Keyfrom = dict. mindex & vendor = & Q = Apple " ] Encoding: nsasciistringencoding error: & Error] datausingencoding: nsutf8stringencoding]; tfhpple * Xpathparser = [[Tfhpple alloc] initwithhtmldata: htmldata]; nsarray * Elements = [xpathparser searchwithxpathquery: @" // Title " ]; // Get the title Nslog ( @" % D " , [Elements count]); tfhppleelement * Element = [elements objectatindex: 0 ]; Nsstring * Content = [Element content]; nsstring * Tagname = [Element tagname]; nsstring * ATTR = [element objectforkey: @" Href " ]; Nslog ( @" Content =% @ " , Content); nslog ( @" Tagname = % @ " , Tagname); nslog ( @" ATTR is =% @ " , ATTR );}
This should be the case when using the project. This method can quickly find the corresponding node, as long as you are familiar with the XPATH rules.
However, I don't know what went wrong. My content cannot be displayed all the time, but the corresponding node is correct because its attributes can be captured. I have found a lot and I don't know why. If I know why, please leave a message!