1

这是我的问题
,我正在尝试删除 HTML 标签,简单地格式化它们(例如每个信息的新行),然后编写其他程序以稍后提取有用的信息。
到目前为止,我已将数据读入一个大字符串数组。
这是我的想法:找到 <> 中的所有标签并将它们替换为空白。在网上查了一下,我看到了一些可以做查找和替换部分的程序。
因为不同的HTML标签有不同的长度,所以我想知道C++中有没有一种方法可以检测标签的任何长度,只要它在<>中。然后更换它们。
这是 HTML 的一个示例代码

<div style="width: 200px;"><strong>Balance Sheets (USD $)<br></strong></div>

我希望在手术后得到:

Balance Sheets (USD $)


有人可以帮忙吗?或者有没有人有更好的主意来处理这个任务?任何帮助是极大的赞赏!

<DOCUMENT>
<TYPE>XML
<SEQUENCE>33
<FILENAME>R2.htm
<DESCRIPTION>IDEA: XBRL DOCUMENT
<TEXT>
<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
    <link rel="StyleSheet" type="text/css" href="report.css"><script type="text/javascript" src="Show.js">/* Do Not Remove This Comment */</script></head>
  <body><span style="display: none;">v2.4.0.6</span><table class="report" border="0" cellspacing="2" id="ID0EQAAG">
      <tr>
        <th class="tl" colspan="1" rowspan="1">
          <div style="width: 200px;"><strong>Balance Sheets (USD $)<br></strong></div>
        </th>
        <th class="th">
          <div>Dec. 31, 2012</div>
        </th>
        <th class="th">
          <div>Dec. 31, 2011</div>
        </th>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_CurrentAssetsAbstract', window );"><strong>Current assets:</strong></a></td>
        <td class="text">&#xA0;<span></span></td>
        <td class="text">&#xA0;<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_CashAndCashEquivalentsAtCarryingValue', window );">Cash and cash equivalents</a></td>
        <td class="nump">$ 106,999<span></span></td>
        <td class="nump">$ 52,109<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccountsReceivableNetCurrent', window );">Accounts receivable</a></td>
        <td class="nump">110,720<span></span></td>
        <td class="nump">61,218<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_AccountsReceivableRelatedParty', window );">Accounts receivable - related party</a></td>
        <td class="nump">1,527<span></span></td>
        <td class="text">&#xA0;<span></span></td>
      </tr>
      <tr class="reu">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AssetsCurrent', window );">Total current assets</a></td>
        <td class="nump">219,246<span></span></td>
        <td class="nump">113,327<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_PropertyPlantAndEquipmentNet', window );">Property and equipment, net</a></td>
        <td class="nump">152,724<span></span></td>
        <td class="nump">160,454<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_CapitalizedSoftwareDevelopmentCostsForSoftwareSoldToCustomers', window );">Capitalized software development costs</a></td>
        <td class="nump">188,371<span></span></td>
        <td class="text">&#xA0;<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_IntangibleAssetsNetExcludingGoodwill', window );">Intangible assets, net</a></td>
        <td class="nump">59,151<span></span></td>
        <td class="nump">59,151<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_OtherAssetsCurrent', window );">Other assets</a></td>
        <td class="nump">11,622<span></span></td>
        <td class="nump">15,470<span></span></td>
      </tr>
      <tr class="rou">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_NoncurrentAssets', window );">Total long term assets</a></td>
        <td class="nump">411,868<span></span></td>
        <td class="nump">235,075<span></span></td>
      </tr>
      <tr class="reu">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_Assets', window );">Total assets</a></td>
        <td class="nump">631,114<span></span></td>
        <td class="nump">348,402<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_CurrentLiabilitiesAbstract', window );"><strong>Current liabilities:</strong></a></td>
        <td class="text">&#xA0;<span></span></td>
        <td class="text">&#xA0;<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccountsPayableCurrent', window );">Accounts payable</a></td>
        <td class="nump">50,866<span></span></td>
        <td class="nump">70,253<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccruedLiabilitiesCurrent', window );">Accrued liabilities</a></td>
        <td class="nump">1,452<span></span></td>
        <td class="nump">6,752<span></span></td>
      </tr>
      <tr class="reu">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_Liabilities', window );">Total current liabilities</a></td>
        <td class="nump">52,318<span></span></td>
        <td class="nump">77,005<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_OtherAccruedLiabilitiesNoncurrent', window );">Other non current liabilities, accrued interest</a></td>
        <td class="nump">7,500<span></span></td>
        <td class="nump">1,500<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_NotesPayableCurrent', window );">Notes payable</a></td>
        <td class="nump">50,000<span></span></td>
        <td class="nump">50,000<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_DueToRelatedPartiesCurrent', window );">Notes payable - related party</a></td>
        <td class="nump">100,000<span></span></td>
        <td class="nump">100,000<span></span></td>
      </tr>
      <tr class="reu">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_OtherLiabilitiesCurrent', window );">Total long-term liabilities</a></td>
        <td class="nump">157,500<span></span></td>
        <td class="nump">151,500<span></span></td>
      </tr>
      <tr class="rou">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_LiabilitiesCurrent', window );">Total liabilities</a></td>
        <td class="nump">209,818<span></span></td>
        <td class="nump">228,505<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_tpoi_ShareholdersEquityAbstract', window );"><strong>Shareholders&#x2019; equity:</strong></a></td>
        <td class="text">&#xA0;<span></span></td>
        <td class="text">&#xA0;<span></span></td>
      </tr>
      <tr class="ro">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_CommonStockValue', window );">Common stock, $0 par value, 30,000,000 shares authorized, 13,312,302 and 9,312,302 shares issued and outstanding at December 31, 2012 and 2011, respectively</a></td>
        <td class="nump">1,542,651<span></span></td>
        <td class="nump">946,151<span></span></td>
      </tr>
      <tr class="re">
        <td class="pl" style="border-bottom: 0px;" valign="top"><a class="a" href="javascript:void(0);" onclick="top.Show.showAR( this, 'defref_us-gaap_AccumulatedDepreciationDepletionAndAmortizationPropertyPlantAndEquipment', window );">Accumulated deficit</a></td>
        <td class="num">(1,145,758)<span></span></td>
        <td class="num">(837,310)<span></span></td>
      </tr>

**Many many HTML code here...... Many Many Many**
                    <td><strong> Name:</strong></td>
                    <td><nobr>us-gaap_StockholdersEquity</nobr></td>
                  </tr>
                  <tr>
                    <td style="padding-right: 4px;"><nobr><strong> Namespace Prefix:</strong></nobr></td>
                    <td>us-gaap_</td>
                  </tr>
                  <tr>
                    <td><strong> Data Type:</strong></td>
                    <td>xbrli:monetaryItemType</td>
                  </tr>
                  <tr>
                    <td><strong> Balance Type:</strong></td>
                    <td>credit</td>
                  </tr>
                  <tr>
                    <td><strong> Period Type:</strong></td>
                    <td>instant</td>
                  </tr>
                </table>
              </div>
            </div>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>
</TEXT>
</DOCUMENT>

代码是这样的,但要长得多

4

3 回答 3

3

由于尚不清楚您正在尝试完成什么,因此下面的解决方案将 HTML 文档分解为单独的标签和文本行。可能有一些特殊情况没有处理,但它确实会处理属性字符串,以防它们包含结束标记分隔符。它写得很快,没有做太多的测试,所以我会把任何必要的修复留给你。它不漂亮但有效,应该足以让你开始。

#include <vector>
#include <string>
#include <iostream>


int main()
{
    std::string html("<div style=\"width: 200px;\"><strong>Balance Sheets (USD $)<br></strong></div>");
    std::vector<std::string>    tags;
    std::vector<std::string>    text;

    for(;;)
    {
        std::string::size_type  startpos;

        startpos = html.find('<');
        if(startpos == std::string::npos)
        {
            // no tags left only text!
            text.push_back(html);
            break;
        }

        // handle the text before the tag    
        if(0 != startpos)
        {
            text.push_back(html.substr(0, startpos));
            html = html.substr(startpos, html.size() - startpos);
            startpos = 0;
        }

        //  skip all the text in the html tag
        std::string::size_type endpos;
        for(endpos = startpos;
            endpos < html.size() && html[endpos] != '>';
            ++endpos)
        {
            // since '>' can appear inside of an attribute string we need
            // to make sure we process it properly.
            if(html[endpos] == '"')
            {
                endpos++;
                while(endpos < html.size() && html[endpos] != '"')
                {
                    endpos++;
                }
            }
        }

        //  Handle text and end of html that has beginning of tag but not the end
        if(endpos == html.size())
        {
            html = html.substr(endpos, html.size() - endpos);
            break;
        }
        else
        {
            //  handle the entire tag
            endpos++;
            tags.push_back(html.substr(startpos, endpos - startpos));
            html = html.substr(endpos, html.size() - endpos);
        }
    }

    std::cout << "tags:\n-----------------" << std::endl;

    // auto, iterators or range based for loop would probably be better but
    // this makes it a bit easier to read.    
    for(size_t i = 0; i < tags.size(); i++)
    {
        std::cout << tags[i] << std::endl;
    }

    std::cout << "\ntext:\n-----------------" << std::endl;
    for(size_t i = 0; i < text.size(); i++)
    {
        std::cout << text[i] << std::endl;
    }
}

上面的代码生成以下输出(在 < 之后没有空格,因为 SO 降价将其解释为应有的 HTML 标记)

标签:

<div style="width:200px;">
<strong>
<br>
</strong>
</div>

文本:

资产负债表(美元)

于 2013-06-04T16:52:30.887 回答
1

正则表达式也不是 HTML 的一种方式。尝试使用一些 HTML 解析库。例如ExpatXerces

这些库可帮助您阅读整个 HTML 或 html 标记、它们的属性等的文本表示。

于 2013-06-04T16:13:17.017 回答
0

一个建议是获取 html 中的所有标签并将它们添加到哈希映射中,然后读取一个标签(在 "<" 之后)并使用哈希映射搜索它并使您的代码采取相应的行动。

于 2013-06-04T16:15:11.983 回答