2011-08-22 54 views
0

我想要從本網頁上的「課程負責人的姓名和電子郵件地址:的XPath檢索兩個查詢

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] 
+0

什麼是'h4'檢查?還有什麼樣的「不起作用」? – AakashM

+0

正在測試一些東西。目前它獲得課程主管名稱,但不是電子郵件地址。它如何檢索兩個? –

回答

0

我不知道到底是什麼XML/XPath的代碼看起來像在Objective C,但我懷疑你已經得到你需要的所有信息,你只需要做更多的事情就可以把它分開。您的XPath檢索節點看起來是這樣的(我已經編輯內容):

<p>Anastassia Angelopolou<br /> 
Email: <a href="mailto:[email protected]">[email protected]</a></p> 

所以,如果你只是問了p節點的文本,你剛纔得到的文本Anastassia Angelopolou,(第一)內文本直到第一個子節點(<br />)。要獲取電子郵件地址,您可以從p節點xpath到./a子節點,並採用文本或@href的值。

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);