cppwebbrower中获取html中table表格的代码

看一下代码:

TStringList  *__fastcall TFrame3::GetTablesRowsText2(CComPtr<IHTMLElement> ele, String strPhone1, String strPhone2)
{
	TStringList *Result = new TStringList();
	
	CComVariant   rowIndex,rowvar2;
	CComPtr <IHTMLElementCollection>   rowcollection;
	long   p, q;
	
	if ( ele ) {

		CComPtr<IHTMLTable> table;
		
		ele->QueryInterface(IID_IHTMLTable,(void**)&table);

		table->get_rows(&rowcollection) ;

		COM_CHECK(rowcollection-> get_length(&p));

		IDispatch* pRowDisp;

		for(int   i=1;i <p;i++)
		{//遍历所有元素
			rowIndex = i;
			HRESULT hr = rowcollection->item(rowIndex, rowvar2, &pRowDisp );
			if ( hr == S_OK )
			{
				IHTMLTableRow * row;
				hr = pRowDisp->QueryInterface(IID_IHTMLTableRow,(void **)&row);
				if ( hr == S_OK )
				{
					IHTMLElementCollection * cellcollection;
					hr = row->get_cells(&cellcollection); 
					LONG cellcount; 
					cellcollection->get_length(&cellcount); 
					for(int celli = 0; celli < cellcount; celli++) 
					{ 
						VARIANT cellIndex; 
						cellIndex.vt = VT_UINT; 
						cellIndex.lVal = celli; 
						VARIANT cellvar2; 
						VariantInit( &cellvar2 ); 

						IDispatch* pCellDisp; 
						HRESULT hr = cellcollection->item(cellIndex, cellvar2, &pCellDisp ); 
						if ( hr == S_OK )
						{ 
							assert(pCellDisp); 
							IHTMLTableCell * cell; 
							hr = pCellDisp->QueryInterface(IID_IHTMLTableCell,(void **)&cell); 
							if ( hr == S_OK ) 
							{ 
								IHTMLElement * elename; 
								hr = cell->QueryInterface(IID_IHTMLElement,(void**)&elename); 
								if (hr == S_OK) 
								{ 
									BSTR name; 
									elename->get_outerText(&name); 
									String tmpname(name);

									if (celli == 0) {
										if (tmpname.SubString(8,4) == strPhone1 || tmpname.SubString(8,4) == strPhone2 ) {
											Result->Add(tmpname);
//											ExeScript( "javascript:selphonetemp(" + IntToStr(rowIndex.intVal) + ")" );
										}

									}

								} 
								cell->Release(); 
							}
							pCellDisp->Release();

						}
					}
				}
			}
		}
		pRowDisp->Release();
	}

  return Result;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值