Use afnetworking to request data and parse with Tfhpple
Parsing content: Example resolves the first item in a recommended title bar on the home page
- (void) wangyi { // NetEase Open Class address nsstring *str=[ nsstring stringwithformat:@ "http://open.163.com/"]; nsurl *url = [ nsurl urlwithstring:[str stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]; // utf-8 nsurlrequest *request = [ nsurlrequest requestwithurl:url]; afhttprequestoperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; // Request data, Set the success and failure callback function [operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject) { // Request down the entire Web page data NSString *html = operation.responseString; // Web page has GBK encoded with UTF8 code, all replaced utf8 NSString *utf8HtmlStr = [html stringbyreplacingoccurrencesofstring:@ "<meta http-equiv=\" content-type\ " content=\" text/html; charset=gbk\ ">" withstring:@ "<meta http-equiv=\" content-type\ " content=\" text/html; charset=utf-8\ ">"]; // tfhpple parsing data, converting it to data NSData *htmlDataUTF8 = [utf8HtmlStr datausingencoding:nsutf8stringencoding]; // Start parsing tfhpple *xpathparser = [[ Tfhpple alloc] initwithhtmldata:htmldatautf8]; // Find all <div class= "G-cell1" // because the first page of the "small recommended" is the use of the class marked, so the main acquisition is the form of small course programs nsarray *elements = [xpathparser searchwithxpathquery:@ "// div[@class = ' g-cell1 '] "]; // does not exist then do not proceed if ([elements count] <= 0) { return; } // The following is just an example of parsing the first course, the loop is all parsed tfhppleelement *first = [ elements firstobject]; // full wording nsarray *arr = [first searchwithxpathquery:@ "//a[1]/@href"]; TFHppleElement *ele = [arr firstObject]; nslog (@ "URL link:%@", [Ele text]); // Merge nslog (@ "Picture link:%@", [[[[first searchwithxpathquery:@]// img/@src "] firstobject] text"); nslog (@ "title:%@", [[[ first searchwithxpathquery:@ "//h5"] firstobject] text]); nslog (@ "subtitle:%@", [[[[First searchwithxpathquery:@]//p "] firstobject] text]); }failure:^ (AFHTTPRequestOperation * Operation, nserror *error) { nslog (@ "error occurred! %@ ", error); }]; // Join queue Start execution nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [queue addoperation:operation];}
Prints the result as
2015-07-10 20:53:10.136 wangyi[3735:210800] website Link: http://open.163.com/movie/2014/3/I/6/MAQ3UM8TJ_MAQ3V5JI6.html
2015-07-10 20:53:10.137 wangyi[3735:210800] Photo Link: http://imgsize.ph.126.net/?enlarge=true&imgurl=http://img1.cache.netease.com/cnews/2015/6/5/ 201506050950000106d.jpg_180x100x1x95.jpg
2015-07-10 20:53:10.137 wangyi[3735:210800] Title: The industry of Japanese love
2015-07-10 20:53:10.137 wangyi[3735:210800] Subtitle: Film crews visit the Red Light District of Japan
Parse the HTML source code for
<div class= "g-cell1" ><a href= "http://open.163.com/movie/2014/3/I/6/MAQ3UM8TJ_MAQ3V5JI6.html" class= " U-cover "target=" _blank "><div class=" shadow rel "><div class=" Mask abs "></div></div></a><a href=" http://open.163.com/movie/2014/3/I/6/MAQ3UM8TJ_MAQ3V5JI6.html ">
IOS NetEase Open Class Analytic example