• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

提取网页所有链接

发布: 2007-7-14 21:11 | 作者: 佚名    | 来源: 网络转载     | 查看: 73次 | 进入软件测试论坛讨论

领测软件测试网 正文
见过“网际快车”的“使用网际快车下载全部链接”这个功能吗?想实现他,我们可以这样做:
     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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网