1

我想解析网页,我使用 htmlunit ,当我运行代码时,我得到以下错误。

import java.net.URL;
import java.util.List;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class scrapImage {

      public static void main(String[] args) throws Exception      { 
          URL url = new URL("http://www.google.com");
          //WebClient webClient = new WebClient(Opera);     
          WebClient webClient = new WebClient();    
          HtmlPage currentPage = (HtmlPage) webClient.getPage(url);    
          //get list of all divs      
          final List<?> images = currentPage.getByXPath("//img");      
          for (Object imageObject : images) {           
              HtmlImage image = (HtmlImage) imageObject;            
              System.out.println(image.getSrcAttribute());       
              }         //webClient.closeAllWindows();           } } 
          }
      }

错误信息:

Exception in thread "main" java.net.UnknownHostException: www.google.com
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:196)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
    at java.net.Socket.connect(Socket.java:530)
    at java.net.Socket.connect(Socket.java:480)
    at java.net.Socket.<init>(Socket.java:377)
    at java.net.Socket.<init>(Socket.java:251)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:346)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:97)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1430)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1388)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:325)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:386)
    at htmlunit.scrapImage.main(scrapImage.java:16)

谁能让我知道上述异常的解决方案。

4

3 回答 3

1

我认为您的网络连接或防火墙的问题可能会阻止 java 程序访问互联网。

于 2012-04-11T05:17:50.107 回答
1

我认为你在代理或防火墙后面。检查您当前系统中的防火墙状态。另外,如果它与代理有关,您可以像这样修改代码。

System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "your proxy host name" );
System.getProperties().put( "proxyPort", "85" );

可能这会对你有所帮助。

于 2012-04-11T05:20:37.307 回答
1

似乎 Internet 连接有问题,或者您使用了代理,

如果是这种情况,请设置代理设置(主机/端口/用户名/密码)。

于 2012-04-11T05:23:04.123 回答