目前我正在解析的html看起来像这样
NSString* markup = [NSString stringWithFormat:@"
<html>
<head>
<title>hi</title>
</head>
<body>
<table id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult'>
<tr>
<th>fail</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</body>
</html>"];
接下来我试图获得每个TR(所以我可以到达每个TR内的每个#2 TD),除了第一个TR里面有TH,所以我想避免这个。
到目前为止,我有以下内容,但请注意它只提取特定的TR / TD组合..不是每个组合:(
NSData *data = [markup dataUsingEncoding:NSUTF8StringEncoding];
TFHpple* xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *elements = [xpathParser search:@"
//table[
@id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult'
]/tr[2]/td[2]
"];
TFHppleElement *element = [elements objectAtIndex:0];
NSString *content = [element content];
[xpathParser release];
[data release];
Xpath新手有什么帮助吗?
答案 0 :(得分:3)
您可以使用:
//table[@id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult']/tr
[position() > 1]/td[2]
甚至更多语义:
//table[@id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult']/tr
[not(th)]/td[2]