明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

提取頁面所有鏈接

[摘要]正文見過“網(wǎng)際快車”的“使用網(wǎng)際快車下載全部鏈接”這個功能嗎?想實現(xiàn)他,我們可以這樣做: IE有幾個有用的接口,我們可以用他來提取網(wǎng)頁所有鏈接 首先是用IHTMLDocument2的get_links,來獲取IHTMLElementCollection接口,再通過IHTMLElem...
正文
見過“網(wǎng)際快車”的“使用網(wǎng)際快車下載全部鏈接”這個功能嗎?想實現(xiàn)他,我們可以這樣做:
     IE有幾個有用的接口,我們可以用他來提取網(wǎng)頁所有鏈接
     首先是用IHTMLDocument2的get_links,來獲取IHTMLElementCollection接口,再通過IHTMLElementCollection來獲取IHTMLAnchorElement,而IHTMLAnchorElement接口的get_href就是我們想要的,通過循環(huán)獲取,我們就可以得到網(wǎng)頁的所有鏈接了!
{
    TCHAR HostName[2*MAX_PATH];
    CComPtr<IDispatch> spDispatch;
    CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
    CComPtr<IHTMLElementCollection> pElementCol;
    CComPtr<IHTMLAnchorElement> pLoct;
    // TODO: Add your control notification handler code here
    int n = m_LinksList.GetItemCount();//GetCount();

    for (int i = 0; i < n; i ++){
        IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_LinksList.GetItemData(i);
        if (pBrowser){
            pBrowser->Release();
        }
    }

    m_LinksList.DeleteAllItems();
    m_LinksNum = 0;
    Log("**************************************************************");
    Log("\r\n");

    if (m_spSHWinds){
        int n = m_spSHWinds->GetCount();
        for (int i = 0; i < n; i++){
            _variant_t v = (long)i;
            IDispatchPtr spDisp = m_spSHWinds->Item(v);


            SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一個IE窗口的智能指針
            if (spBrowser){
               //獲取IHTMLDocument2接口
                if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
                    pDoc2 = spDispatch;
                if(pDoc2!=NULL)
                {

                    //獲取IHTMLElementCollection接口
                    if (SUCCEEDED(pDoc2->get_links(&pElementCol)))
                    {
                        //    AfxMessageBox("IHTMLElementCollection");
                        long p=0;
                        if(SUCCEEDED(pElementCol->get_length(&p)))
                            if(p!=0)
                            {   
                                m_LinksNum = m_LinksNum+p;
                                UpdateData(FALSE);
                                for(long i=0;i<=(p-1);i++)
                                {
                                    BSTR String;
                                    _variant_t index = i;
                                    if(SUCCEEDED(pElementCol->item( index, index, &spDispatch)))             

                                   //查找IHTMLAnchorElement接口
                                    if(SUCCEEDED(spDispatch->QueryInterface( IID_IHTMLAnchorElement,(void **) &pLoct)))
                                    //取得鏈接
                                    pLoct->get_href(&String);
                                    ZeroMemory(HostName,2*MAX_PATH);
                                    lstrcpy(HostName,_bstr_t(String));

                                   //插入鏈接到list中
                                    m_LinksList.InsertItem(i,HostName);
                                    m_LinksList.SetCheck(i,TRUE);
                                   
                                    pLoct->get_hostname(&String);
                                    ZeroMemory(HostName,2*MAX_PATH);
                                    lstrcpy(HostName,_bstr_t(String));
                                    if(lstrlen(HostName))
                                    {
                                        m_LinksList.SetItemText(i,1,HostName);
                                        Log(HostName );
                                        Log("\r\n");
                                    }
                                    
                                }
                            }
                    }

                }

            }
        }
    }
}

    本程序在VC7+WINXP下編譯通過,詳細(xì)請看源代碼!
  TanXin79@163.net,謝謝閱讀,文章源代碼如下:

http://www.vchelp.net/ASP/ibr_upload/543.rar


標(biāo)簽:提取頁面所有鏈接 

相關(guān)文章