IPhone: The first hpple test to parse the third library of HTML

Source: Internet
Author: User

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!

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.