我使用以下代码在HTML文件中查找某行代码:
NSURL *requestTimetableURL = [NSURL URLWithString:@"http://www.dhsb.org/index.phtml?d=201435"];
NSLog(@"Loaded Timetable");
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestTimetableURL
encoding:NSASCIIStringEncoding
error:&error];
[webView loadHTMLString:page baseURL:requestTimetableURL];
NSString* Period1;
NSScanner *htmlScanner = [NSScanner scannerWithString:page];
[htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period1];
period1label.text= Period1;
NSLog(@"Collected Period 1 Data: %@", Period1);
NSScanner *htmlScanner2 = [NSScanner scannerWithString:page];
NSString* Period2;
[htmlScanner2 scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner2 scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner2 scanUpToString:@"</FONT>" intoString:&Period2];
period2label.text= Period2;
NSLog(@"Collected Period 2 Data: %@", Period2);
您会注意到要查找的两个字符串都是相同的。这是因为没有什么可以实际区分两行代码。有两个匹配:
<P align=center><STRONG><FONT color=#c00000>
在我的代码中。如何查找“Period1”的第一场比赛和“Period2”的第二场比赛?
谢谢!
答案 0 :(得分:0)
在使用第二台扫描仪之前,请将scanLocation
设置为超出短语第一个实例的开头,如下所示:
[htmlScanner2 setScanLocation:(htmlScanner1.scanLocation + 1)];
NSURL *requestTimetableURL = [NSURL URLWithString:@"http://www.dhsb.org/index.phtml?d=201435"];
NSLog(@"Loaded Timetable");
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestTimetableURL
encoding:NSASCIIStringEncoding
error:&error];
[webView loadHTMLString:page baseURL:requestTimetableURL];
NSString* Period1;
NSScanner *htmlScanner = [NSScanner scannerWithString:page];
[htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period1];
period1label.text= Period1;
NSLog(@"Collected Period 1 Data: %@", Period1);
NSString* Period2;
[htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period2];
period2label.text= Period2;
NSLog(@"Collected Period 2 Data: %@", Period2);