이 작동합니다 : 내 코드는이 URL을 구문 분석합니다.
HTML을 보면, 저자가 CSS를 철자하는 방법을 모르기 때문에 원본 전체에 글꼴 태그가 있습니다.
tutorial.title = [[element firstChild] content];
을하지만 그것은 오히려 항목의 대부분은 firstChild
이 text
하지 않기 때문에, 여기에 작동하지만하지 않습니다 그래서 분명히 우수한 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
...
출처
2013-05-06 22:32:38
Rob
엑스 코드 구문 분석하지 않습니다 HTML. OS X os iOS 용입니까? 이에 따라 "코코아/코코아 터치 사용"으로 바꾸어야합니다. –
질문을 다시 작성했습니다. –