html - iOS Hpple HTML 解析

标签 html ios objective-c uitableview hpple

我需要在我的应用程序中将网站的内容解析为表格 View 。我试过 hpple 并且在一些测试用例中它有效。但在我的具体情况下,我无法让它工作...... HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
   <head>
      <link rel="stylesheet" type="text/css" href="willi.css">
      </link><script src="style.js" type="text/javascript"></script>
      <title>Homepage</title>
   </head>
   <body>
      <a name="oben"/>
         <h1>Date</h1>
         <br />
      <a href="#07.07.2015">07.07.2015</a><br />
      <a href="#07.08.2015">07.08.2015</a><br />
      <a name="07.07.2015">
         <hr />
      </a>
      <p class="page" style="text-align:left">
      <h2>Date Tue, 7.7.2015</h2>
      created: 7.7. 16:35 </p>
      <p class="page" style="text-align:left">
      <table class="F" border-width="3">
         <colgroup>
            <col width="899"/>
         </colgroup>
         <tr class="F">
            <th rowspan="1" class="F">
               ***&nbsp;&nbsp; Version 1&nbsp;&nbsp; ***
            </th>
         </tr>
         <tr class="F">
            <th rowspan="1" class="F"></th>
         </tr>
         <tr class="F">
            <th rowspan="1" class="F">
               Testmessage 1
            </th>
         </tr>
         <tr class="F">
            <th rowspan="1" class="F">
               Testmessage 2
            </th>
         </tr>
         <tr class="F">
            <th rowspan="1" class="F">
               Testmessage 3
            </th>
         </tr>
         <tr class="F">
            <th rowspan="1" class="F"></th>
         </tr>
         <tr class="F">
            <th rowspan="1" class="F">
               Testmessage 4
            </th>
         </tr>
      </table>
      </p>
      <p class="seite" style="text-align:left">
      <h4>List:</h4>
      <table class="k" border-width="3">
         <tr>
            <th width="50">
               Team
            </th>
            <th width="50">
               &nbsp;Name
            </th>
            <th width="50">
               Nr.
            </th>
            <th width="50">
               &nbsp;Mate
            </th>
            <th width="50">
               Spot
            </th>
            <th width="50">
               &nbsp;Map
            </th>
            <th width="150"></th>
         </tr>
         <tr class="k">
            <th rowspan="5" class="k">
               A
            </th>
            <td>
               &nbsp;First
            </td>
            <td>
               3
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Second
            </td>
            <td>
               4
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Sie
            </td>
            <td>
               8
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Sie
            </td>
            <td>
               9
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Es
            </td>
            <td>
               10
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr class="k">
            <th rowspan="1" class="k">
               B
            </th>
            <td>
               &nbsp;Red
            </td>
            <td>
               11
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
      </table>
      </p>
      <hr />
      <a name="07.08.2015">
         <hr />
      </a>
      <p class="page" style="text-align:left">
      <h2>Date Thu, 8.7.2015</h2>
      created: 7.7. 16:35 </p>
      <p class="page" style="text-align:left">
      <table class="F" border-width="3">
         <colgroup>
            <col width="899"/>
         </colgroup>
         <tr class="F">
            <th rowspan="1" class="F">
               ***&nbsp;&nbsp; Version 1&nbsp;&nbsp; ***
            </th>
         </tr>
      </table>
      </p>
      <p class="page" style="text-align:left">
      <h4>List:</h4>
      <table class="k" border-width="3">
         <tr>
            <th width="50">
               Team
            </th>
            <th width="50">
               &nbsp;Name
            </th>
            <th width="50">
               Nr.
            </th>
            <th width="50">
               &nbsp;Mate
            </th>
            <th width="50">
               Spot
            </th>
            <th width="50">
               &nbsp;Map
            </th>
            <th width="150"></th>
         </tr>
         <tr class="k">
            <th rowspan="5" class="k">
               C
            </th>
            <td>
               &nbsp;Dnk
            </td>
            <td>
               1
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Es
            </td>
            <td>
               1
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Dnk
            </td>
            <td>
               2
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;Esta
            </td>
            <td>
               2
            </td>
            <td>
               &nbsp;
            </td>
            <td></td>
            <td>
               &nbsp;
            </td>
            <td>
               &nbsp;Test
            </td>
         </tr>
         <tr>
            <td>
               &nbsp;SWB
            </td>
            <td>
               6
            </td>
            <td>
               &nbsp;Naau
            </td>
            <td>
               F
            </td>
            <td>
               &nbsp;Test
            </td>
            <td>
               &nbsp;
            </td>
         </tr>
      </table>
      </p>
      <hr />
   </body>
</html>

该页面包含两个主要元素 ( <table></table> ),我想用它们来填充我的 UITableView。

我的目标是每个表有一个部分,每个部分内包含表格的所有内容。 节标题名称应为“日期”。

TFHpple *Parser = [TFHpple hppleWithHTMLData:HtmlData];

NSString *XpathQueryString = @"/html/body/a";
NSArray *Nodes = [Parser searchWithXPathQuery:XpathQueryString];

for (TFHppleElement *element in Nodes) {
    NSString *temp = [[element firstChild] content];
    if (temp.length == 10) {
        [Day addObject:temp];
    }
}

在我的 NSMutableArray *Day我保存了日期,这很好用。我得到 2 个名称正确的部分。 但是当我尝试接收表格内容时,我无法让它工作...... 我想要类似的东西

tableElement* newElement = [[tableElement alloc] init];
newElement.day = @"07.07.2015";
newElement.team = @"A";
newElement.name = @"First";
newElement.nr = @"3";
newElement.mate = @"";
newElement.spot = @"";
newElement.map = @"";
newElement.status = @"Test";

然后我可以存储所有 newElement (s) 日期一在一个数组中,日期二的所有元素在另一个元素中。

edit: e.g newElement.day = @"07.07.2015"; of course needs to be something like newElement.day = [[hppleparse firstChild] content];

最佳答案

这可以通过 HTMLKit 轻松实现.

以下是您可以使用您提供的 HTML 对其执行哪些操作的几个示例:

HTMLDocument *document = [HTMLDocument documentWithString:html];
NSMutableArray *days = [ NSMutableArray array];
NSArray *links = [document querySelectorAll:@"a"];
for (HTMLElement *link in links) {
  if (link.textContent.length == 10) {
    [days addObject:link.textContent];
  }
}

// For example you can:
// Get all <tr> elements that are children of the table with className 'k'
NSArray *tableKRows = [document querySelectorAll:@"table.k > tr"];

// Get all <td> elements that are descendants of the table with className 'k'
NSArray *tableKData = [document querySelectorAll:@"table.k td"];

// Collect content of all <td> elements in `array`
NSMutableArray *array = [NSMutableArray array];
for (HTMLElement *td in tableKData) {
  NSString *content = [td.textContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  [array addObject:content];
}

如果您需要任何进一步的帮助,请告诉我。

HTMLKit 是一个支持 CSS3 选择器的纯 Objective-C HTML 解析器。它不是 libxml 或任何其他库的包装器,而是一个完整的符合 WHATWG HTML 规范的实现。

关于html - iOS Hpple HTML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305804/

相关文章:

javascript - 在 error.responeText 中取回 JSON 数据

javascript - 使用 jquery 为 <select multiple> 设置选定属性

javascript - jQuery 星级 slider 过滤器

objective-c - 分配和释放问题

html - 如何从 Firefox 中的选择元素中删除箭头

ios - UITableView 在 IOS 7 中可见,但在 IOS 6 中不可见

ios - XCODE 中的 Json.Net 错误 - System.Reflection.Emit

ios - Nest-API 恒温器设备未显示在 REST 请求中

ios - YTPlayerView youtube-ios-player-helper 暂停不工作

ios - 如何在特定点停止 NSTimer?