XPath检索两个查询

时间:2011-08-22 12:31:12

标签: html objective-c xcode xpath html-parsing

我想从此网页中检索“课程负责人”姓名和电子邮件地址:

http://www.westminster.ac.uk/schools/computing/undergraduate/computer-games-development/bsc-honours-computer-games-development

如何实现这一目标?

我试图在“课程内容”之后检索第一个<p>但是没有完成工作..

"//div[starts-with(@id,'content_div')]/h3[.='Course Content']/following-sibling::p[1]

2 个答案:

答案 0 :(得分:0)

我不确切知道Objective C中的xml / xpath代码是什么,但我怀疑你 已经获得了你需要的所有信息,你只需要做更多的事情就可以了把它拆开你的xpath检索的节点看起来像这样(我编辑了内容):

<p>Anastassia Angelopolou<br />
Email: <a href="mailto:agelopa@wmin.ac.uk.invalid">agelopa@wmin.ac.uk.invalid</a></p>

因此,如果您只是要求p节点的文本,您只需获取文本Anastassia Angelopolou,(第一个)内部文本到第一个子节点(<br />)。要获取电子邮件地址,您可以从p节点到./a子节点xpath,并获取文本或@href的值。

答案 1 :(得分:0)

由于在您要查找的任何一个值上没有真正唯一的标识标记,我会跳过xpath并创建一个脏的小黑客。

// get the HTML code.
NSString * getURL = [NSString stringWithFormat:@"http://www.westminster.ac.uk/schools/computing/undergraduate/computer-games-development/bsc-honours-computer-games-development"];
NSData * htmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:getURL]];
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];


//seperate the HTML code by the unique HTML line of "<h3>Course Leader</h3>"
NSArray *tempArray = [htmlString componentsSeparatedByString:@"<h3>Course Leader</h3>"];
NSString * tempString1 = [[tempArray objectAtIndex:1]description];

//get Name
NSArray * tempArray2 = [tempString1 componentsSeparatedByString:@"<br />"];

//set name
NSString * nameString = [[tempArray2 objectAtIndex:0]description];
//clean up name string
nameString = [nameString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
nameString = [nameString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
nameString = [nameString stringByReplacingOccurrencesOfString:@"<p>" withString:@""];

//get Email 
NSArray * emailArray = [tempString1 componentsSeparatedByString:@">"];

//set email string
NSString * emailString = [[emailArray objectAtIndex:3]description];
//clean up email string
emailString = [emailString stringByReplacingOccurrencesOfString:@"</a" withString:@""];

NSLog(@"Results: Name = %@  Email = %@",nameString,emailString);