明辉站/网站教程/内容

提取页面所有链接

网站教程2024-02-06 阅读
[摘要]正文见过“网际快车”的“使用网际快车下载全部链接”这个功能吗?想实现他,我们可以这样做: IE有几个有用的接口,我们可以用他来提取网页所有链接 首先是用IHTMLDocument2的get_links,来获取IHTMLElementCollection接口,再通过IHTMLElem...
正文
见过“网际快车”的“使用网际快车下载全部链接”这个功能吗?想实现他,我们可以这样做:
     IE有几个有用的接口,我们可以用他来提取网页所有链接
     首先是用IHTMLDocument2的get_links,来获取IHTMLElementCollection接口,再通过IHTMLElementCollection来获取IHTMLAnchorElement,而IHTMLAnchorElement接口的get_href就是我们想要的,通过循环获取,我们就可以得到网页的所有链接了!
{
    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下编译通过,详细请看源代码!
  TanXin79@163.net,谢谢阅读,文章源代码如下:

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

……

相关阅读