gpt4 book ai didi

iOS 解析内容需要 hpple 帮助

转载 作者:行者123 更新时间:2023-11-29 03:26:39 24 4
gpt4 key购买 nike

我目前正在尝试自学如何在 iOS 中筛选剪贴簿留言,并已学会如何在 Android 上执行此操作。

我正在使用 hpple 库。

我目前正在努力使用 hpple 复制我在 Android 上的内容,因此我正在寻找有关如何正确使用 hpple 来解析我的 HTML 内容的一些指导。

我目前正在尝试从我的 HTML 网站解析以下内容:

<table class="tableForAppContent">     

<tr>
<td nowrap="nowrap">
<a href='testLink'>CODE</a> MyTestCode</td>
<td nowrap>
<a href='testLink'>Number 123</a></td>
<td></td>
<td>Company Name</td>
<td nowrap>
11:10 AM
</td>
<td class="tableList" nowrap>
</td>
<td>
</td>
<td nowrap>
Status of company
<br />
</td>
<td>
</td>
</tr>

</table>

我需要能够获取您在 HTML 中看到的所有文本值,因此我需要能够获取以下值:“CODE MyTestCode”、“Number 123”、“Company Name”、“11:10 AM"和 "公司状况"。

这是我目前的代码:

NSURL *url = [NSURL URLWithString:@"MyTestSite.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval: 30.0]; // Will timeout after 30 seconds
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

if (data != nil && error == nil)
{
NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:data encoding:@"NSASCIIStringEncoding"];
NSString *tutorialsXpathQueryString = @"//table[@class='tableForContent']//td";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
NSLog(@"%@", [[element firstChild] content]);

}
}
else
{
// There was an error, alert the user
}
}];

我无法为以下代码行找出正确的 XPath 查询字符串

NSString *tutorialsXpathQueryString = @"//table[@class='tableForContent']//td";

无论我尝试什么,我一次只能找到一个元素,所以我只能得到“公司名称”值,但没有别的。

任何人都可以帮助查询字符串吗?

最佳答案

尝试使用 XPath 表达式

//table[@class='tableForContent']//*[normalize-space(text()) != '']

它应该给出包含非全空白文本的所有节点。

编辑

上面的解决方案分割格式<td>条目进入多个节点,这不是您想要的。因此,事实上,就粒度级别而言,您原来的 XPath 似乎是正确的方法。

以下 XPath

//table[@class='tableForAppContent']//td[* or normalize-space(text()) != '']

给你“权利”<td>条目,也就是说,只有那些包含文本本身或至少一个子节点的条目,这应该导致所有非空节点。

但是,结果节点集由具有子结构的节点组成,这意味着它们既包含文本节点,也包含具有文本节点的子节点。由于您使用这些结果节点集作为 XPath 和调用例程(在 Objective C 中?)之间的接口(interface),您可能必须自己从该子树中提取文本元素并将它们连接起来。也许您可以使用一些库例程来实现这一点。如果没有,您始终可以通过递归遍历结果节点树来完成。

关于iOS 解析内容需要 hpple 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com