`
suiyuan0808
  • 浏览: 152454 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

用HtmlUnit和httpClient抓施华洛世奇网站图片和动画<二>

    博客分类:
  • Java
阅读更多

try
    {
     mainPage = webClient.getPage(url);
    } catch (Exception e)
    {
     log.error(e.getMessage(), e);
    }
    if (mainPage != null)
    {
     HtmlElement paginationContainer = mainPage
       .getElementById("paginationContainer");
     if (paginationContainer != null)
     {
      url = url + "/all-1";
      try
      {
       mainPage = webClient.getPage(url);
      } catch (Exception e)
      {
       log.error(e.getMessage(), e);
      }
     }
HtmlElement categories = mainPage.getElementById("categories");
     if (categories == null)
     {
      HtmlElement products = mainPage.getElementById("products");
      if(products!=null)
      {
       List<HtmlElement> productDivList = products.getElementsByTagName("div");
       if(productDivList!=null&&productDivList.size()>0)
       {
        for(HtmlElement proDiv:productDivList)
        {
         List<HtmlElement> subList = proDiv.getElementsByTagName("div");
         if(subList!=null)
         {
          for(HtmlElement dt:subList)
          {
           String classt = dt.getAttribute("class");
           if (classt != null && classt.equals("productImg"))
           {
            List<HtmlElement> subAList = proDiv.getElementsByTagName("a");
            if(subAList!=null&&subAList.size()>0)
            {
             HtmlElement ae=subAList.get(0);
             String href = ae.getAttribute("href");
             thirdPageUrls.add(href);
            }
            break;
           }
          }
         }
        }
       }
      }
     }
     else
     {
      List<HtmlElement> divList = categories.getElementsByTagName("div");
         if (divList != null && divList.size() > 0)
         {
       for (HtmlElement div : divList)
       {
        List<HtmlElement> tempDivs = div
          .getElementsByTagName("div");
        if (tempDivs != null && tempDivs.size() > 0)
        {
         for (HtmlElement div1 : tempDivs)
         {
          List<HtmlElement> aList = div1
            .getElementsByTagName("a");
          HtmlAnchor a = (HtmlAnchor) aList
            .get(0);
          String link = a.getHrefAttribute();
          secondPageUrls.add(link);

         }

        }
       }
      }
     }
    

    }
   }
  }
  log.error("第二层抓取结束..........");
  log.error("目前抓取到的第二层URL个数为:"+secondPageUrls.size());

 

int count=0;
  
  for (String url : secondPageUrls)
  {
   count++;
   log.error("正在抓取第二层的第"+count+"个URL:"+url);
   HtmlPage mainPage = null;
   try
   {
    mainPage = webClient.getPage(url);
   } catch (Exception e)
   {
    log.error(e.getMessage(), e);
   }
   log.error("抓取URL完成:"+url+",正在分析URL"+url+"+结果的URL");
   if (mainPage != null)
   {
    HtmlElement paginationContainer = mainPage
      .getElementById("paginationContainer");
    if (paginationContainer != null)
    {
     url = url + "/all-1";
    }
    HtmlElement products = mainPage.getElementById("products");
    if (products != null)
    {
     List<HtmlElement> list = products
       .getElementsByTagName("div");
     ;
     if (list == null || list.size() == 0)
     {
      continue;
     }
     for (HtmlElement h : list)
     {
      String cls = h.getAttribute("class");
      if (cls == null || !cls.equals("productName"))
      {
       continue;
      }
      List<HtmlElement> links = h.getElementsByTagName("a");
      if (links != null && links.size() > 0)
      {
       HtmlAnchor htmlAnchor = (HtmlAnchor) links.get(0);
       String linkStr = htmlAnchor.getHrefAttribute();
       thirdPageUrls.add(linkStr);
       log.error(linkStr);
      }

     }
    }

   }
  }
  log.error("第二层抓取结束..........");
  secondPageUrls.clear();
  secondPageUrls = null;
  
   count=0;
  log.error("目前抓取到的第三层URL个数为:"+thirdPageUrls.size());
  String urlPrix="
http://www.swarovski-crystallized.com/jewelry/us/";
 for (String url : thirdPageUrls)
  {
   count++;
   log.error("正在抓取第三层的第"+count+"个URL:"+url);
   HtmlPage mainPage = null;
   try
   {
    mainPage = webClient.getPage(url);
   } catch (Exception e)
   {
    log.error(e.getMessage(), e);
   }
   if (mainPage != null)
   {
    log.error("抓取URL完成:"+url+",正在分析URL"+url+"+结果");
    int indexC=url.indexOf(urlPrix);
    int indexD=url.indexOf("?");
    String dirStr=url.substring(indexC+urlPrix.length(), indexD);
    String regEx = "/";
//    Pattern p = Pattern.compile(regEx);
//    Matcher m = p.matcher(dirStr);
    

//哦哦哦,建立文件夹准备把抓到数据放在里面
    dirStr=replece( regEx,"\\\\",dirStr);
    dirStr = "D:\\swaroski\\"+dirStr;
    File   file   =   new   File(dirStr);  
    if(file.isDirectory())
    {
     dirStr=dirStr+"\\"+count;
     file   =   new   File(dirStr); 
    }

 file.mkdirs();

Product product=new Product();
    product.setLocalDir(dirStr);
    
    product.setPageUrl(url);
    HtmlElement rightCol = mainPage.getElementById("rightCol");
    String title = null;
    String description = null;
    String packingUnit = null;
    if (rightCol != null)
    {
     HtmlElement headlineDiv = rightCol
       .getElementById("headline");
     if (headlineDiv != null)

。。。。。。。。。。。。。。。。。。。。。。。。

log.error("完成:"+url+",分析结果");
     try
     {
      swaroSkiDAO.addProduct(product);
     } catch (Exception e)
     {
      log.error(e.getMessage(), e);
     }
     log.error("完成保存结果");
     
     for(String downloadUrl:resourceUrlList)
     {
      int index6=downloadUrl.lastIndexOf("/");
      String fileName=downloadUrl.substring(index6+1);
      String dirStr2=dirStr+"\\"+fileName;
         File storeFile = new File(fileName);
         if(storeFile.exists())
         {
          continue;
         }
      SaveFileThread runable=new SaveFileThread(dirStr2,downloadUrl,sem);
      pools.submit(runable);
      log.error("开始提交下载文件:"+downloadUrl);
      try
      {
       Thread.sleep(2500);
      } catch (InterruptedException e)
      {
      }

}

 

 

//啊哈哈,这个就是去拿下图片和动画的线程,结束

class SaveFileThread implements Runnable
 {
  private String fileName;
  private String downloadUrl;
  private Semaphore sem;
          public SaveFileThread(String fileName,String downloadUrl,Semaphore sem)
          {
           this.fileName=fileName;
           this.downloadUrl=downloadUrl;
           this.sem=sem;
          }
  public void run()
  {
            HttpClient client = new HttpClient(); 
           GetMethod get = new GetMethod(downloadUrl); 
           FileOutputStream output=null;
           try
     {
        client.executeMethod(get);
         File storeFile = new File(fileName); 
             output = new FileOutputStream(storeFile); 
             output.write(get.getResponseBody());
             output.flush();
     } catch (Exception e)
     {
      log.error(e.getMessage(), e);
     }

1
1
分享到:
评论

相关推荐

    HttpClient4.1.2 & HtmlUnit2.9 处理文件下载

    NULL 博文链接:https://caerun.iteye.com/blog/1338956

    htmlunit:HtmlUnit是“用于Java程序的GUI更少的浏览器”

    &lt;artifactId&gt;htmlunit&lt;/artifactId&gt; &lt;version&gt;2.48.0&lt;/version&gt; &lt;/dependency&gt; 概述 HtmlUnit是“用于Java程序的GUI更少的浏览器”。 它为HTML文档建模,并提供一个API,使您可以调用页面,填写表单,单击链接等,...

    htmlunit-driver:适用于HtmlUnit无头浏览器的WebDriver兼容驱动程序

    HtmlUnitDriver ... &lt; artifactId&gt;htmlunit-driver&lt;/ artifactId&gt; &lt; version&gt;2.49.1&lt;/ version&gt; &lt;/ dependency&gt; 用法 简单的 您可以简单地使用HtmlUnit驱动程序类中的构造函数之一 // simple case - no j

    htmlunit-toutiao.zip

    htmlunit头条搜索操作

    htmlunit下载资源示例

    通过htmlunit 下载网站资源的简单示例 htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。 项目可以模拟浏览器运行,被誉为java浏览器的开源实现。是一个没有界面的...

    httpclient相关的jar包

    HTTP 协议可能是现在 Internet 上...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.2 (GA)

    Commons-HTTPClient组件介绍与使用

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。

    针对 httpclient4.* 绕验证码获取公司信息 包括 jsoup网页信息的爬虫及htmlUnit对动态网站信息的抓取

    针对 httpclient4.* 绕验证码获取公司信息 包括 jsoup网页信息的爬虫及htmlUnit对动态网站信息的抓取

    httpclient4.2.1

    HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java ...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。

    htmlUnit所需jar包

    htmlUnit所需工具包,htmlUnit,htmlUnit,htmlUnit,htmlUnit,htmlUnit

    htmlunit2.8 + jsoup1.7网站数据抓取

    htmlunit2.8 + jsoup1.7各种网站上的数据抓取。

    HttpClient API CHM

    HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java ...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient,

    httpclient-4.5所需jar包

    HTTP 协议可能是现在 Internet 上使用...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.5 .6

    HttpClient以及获取页面内容应用

    HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 下载地址:  http://hc.apache.org/downloads.cgi 1.2特性 1. 基于标准、纯净的java语言。...

    httpclient-4.5所需所有jar包

    HTTP 协议可能是现在 Internet 上使用得...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.5

    htmlunit-2.14

    java 开发 htmlunit官方jar包和api文档

    HttpClient使用示例教程

    HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 适用人群:JavaWeb开发者 使用场景:在Java后端程序中,模拟浏览器端进行Web请求。 其它:...

    HttpClient

    HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.5 (GA) (2015-09-11)

    Htmlunit2.23-bin.zip

    htmlunit jar zip 2.23 用于html爬虫

Global site tag (gtag.js) - Google Analytics