2013-04-16 2 views
1

TFHPPLE 파서를 사용하여 구문 분석을 시도하고 있습니다 : [TRUE (1)]) 테이블에. TD의 모든 속성은 동일합니다. 나는 TD를 구별 할 수 없다.
모든 HTML 코드를 가져올 수 있지만 각 TR에서 첫 번째 TD를 가져 오지 못했습니다. // 3 (코드에 있음) 자습서 노드가 비어있는 후.HTML 테이블 xcode 구문 분석

NSLog(@"Nodes are : %@",[tutorialsNodes description]); 

의 출력은

 
Practice1[62351:c07] Nodes are :(). 

내가 뭐가 잘못 볼 수있다. 어떤 도움을 주시면 감사하겠습니다. 대신 @"//TR/TD"@"//tr/td"를 사용하는 경우

NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.neiu.edu/~neiutemp/PhoneBook/alpha.htm"]; 
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl]; 

// 2 
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData]; 

// 3 
NSString *tutorialsXpathQueryString = @"//TR/TD"; 
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString]; 
NSLog(@"Nodes are : %@",[tutorialsNodes description]); 
// 4 
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0]; 
for (TFHppleElement *element in tutorialsNodes) { 
    // 5 
    Tutorial *tutorial = [[Tutorial alloc] init]; 
    [newTutorials addObject:tutorial]; 

    // 6 
    tutorial.title = [[element firstChild] content]; 

    // 7 
    tutorial.url = [element objectForKey:@"href"]; 

    NSLog(@"title is: %@",[tutorial.title description]); 
} 

// 8 
_objects = newTutorials; 
[self.tableView reloadData]; 
+0

엑스 코드 구문 분석하지 않습니다 HTML. OS X os iOS 용입니까? 이에 따라 "코코아/코코아 터치 사용"으로 바꾸어야합니다. –

+0

질문을 다시 작성했습니다. –

답변

2

이 작동합니다 : 내 코드는이 URL을 구문 분석합니다.

HTML을 보면, 저자가 CSS를 철자하는 방법을 모르기 때문에 원본 전체에 글꼴 태그가 있습니다.

tutorial.title = [[element firstChild] content]; 

을하지만 그것은 오히려 항목의 대부분은 firstChildtext하지 않기 때문에, 여기에 작동하지만하지 않습니다 그래서 분명히 우수한 Hpple tutorial by Matt Galloway on Ray Wenderlich's site에서 가져온 코드의 다음 라인은 말한다 그것은 font 태그입니다.

TFHppleElement *subelement = [element firstChild]; 
if ([[subelement tagName] isEqualToString:@"font"]) 
    subelement = [subelement firstChild]; 
tutorial.title = [subelement content]; 

을 또는, 당신은 대신 @"//tr/td/font" 대신 @"//tr/td"를 검색 할 수 있습니다 : 그래서 당신은 글꼴 태그과 같이이었다 있는지 확인할 수 있습니다. 여기에 많은 접근 방법이 있습니다. 트릭 (모든 HTML 구문 분석과 마찬가지로)은 페이지를 사소한 수정이 필요 없도록 합리적으로 만들 것입니다.

물론 HTML에는 URL이 없으므로이 코드는 여기에 적용되지 않습니다.

어쨌든, 이것이 당신을 끌어들일 수 있기를 바랍니다.


당신은 문제가보고, 그래서 난 그냥 더 완전한 코드 샘플 공급 거라고 생각 :

NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.neiu.edu/~neiutemp/PhoneBook/alpha.htm"]; 
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl]; 

TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData]; 

NSString *tutorialsXpathQueryString = @"//tr/td"; 
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString]; 

if ([tutorialsNodes count] == 0) 
    NSLog(@"nothing there"); 
else 
    NSLog(@"There are %d nodes", [tutorialsNodes count]); 

NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0]; 
for (TFHppleElement *element in tutorialsNodes) { 

    Tutorial *tutorial = [[Tutorial alloc] init]; 
    [newTutorials addObject:tutorial]; 

    TFHppleElement *subelement = [element firstChild]; 
    if ([[subelement tagName] isEqualToString:@"font"]) 
     subelement = [subelement firstChild]; 
    tutorial.title = [subelement content]; 

    NSLog(@"title is: %@", [tutorial.title description]); 
} 

다음과 같은 출력 산출 :

 
2013-05-10 19:39:42.027 hpple-test[33881:c07] There are 10773 nodes 
2013-05-10 19:39:42.028 hpple-test[33881:c07] title is: A 
2013-05-10 19:39:46.027 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:46.698 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:47.333 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:47.827 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:48.358 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:49.133 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:49.775 hpple-test[33881:c07] title is: Abay, Hiwet B 
2013-05-10 19:39:50.326 hpple-test[33881:c07] title is: H-Abay 
2013-05-10 19:39:50.992 hpple-test[33881:c07] title is: 773-442-5140 
2013-05-10 19:39:51.597 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:52.092 hpple-test[33881:c07] title is: Controller 
2013-05-10 19:39:52.598 hpple-test[33881:c07] title is: E 
2013-05-10 19:39:53.149 hpple-test[33881:c07] title is: 223 
2013-05-10 19:39:55.040 hpple-test[33881:c07] title is: Abbruscato, Terence 
2013-05-10 19:39:55.806 hpple-test[33881:c07] title is: T-Abbruscato 
2013-05-10 19:39:56.525 hpple-test[33881:c07] title is: 773-442-5339 
... 
+0

감사합니다. Rob. 그것은 작동합니다. 하지만 : ** NSLog (@ "제목은 % @", [tutorial.title description]), ** got : ** 2013-05-10 14 : 23 : 15.375 Practice1 [78352 : c07] 제목은 다음과 같습니다. : 출력으로 (null) **. xpathQueryString으로 tr/td/font를 사용했습니다. 더 이상 제안을하시기 바랍니다 !! –

+0

@MitraPatel보다 완벽한 코드 샘플을 사용하여 답변을 업데이트했습니다. – Rob

+0

나는 고맙습니다. –