如何为Jsoup添加代理支持?

Him*_*shu 39 java jsoup

我是Java的新手,我的第一个任务是解析大约10,000个URL并提取一些信息,为此我使用Jsoup并且它工作正常.

但现在我想为它添加代理支持.代理也有用户名和密码.

谁能帮我这个?

小智 70

您可以轻松设置代理

System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "1080");
Document doc = Jsoup.connect("www.google.com").get();
Run Code Online (Sandbox Code Playgroud)

  • 我似乎无法让这个设置起作用.无论我将proxyHost设置为IP,Jsoup.connect都会成功完成. (9认同)
  • @ jln646v以下答案描述了如何使用代理设置Jsoup:http://stackoverflow.com/a/34943161/363573. (4认同)
  • 我同意@ jln646v。最好使用`Jsoup.proxy(proxy)`。 (2认同)

Ste*_*han 38

Jsoup 1.9.1及以上:(推荐方法)

// Fetch url with proxy
Document doc = Jsoup //
               .connect("http://www.example.com/") //
               .proxy("127.0.0.1", 8080) // sets a HTTP proxy
               .userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2") //
               .header("Content-Language", "en-US") //
               .get();
Run Code Online (Sandbox Code Playgroud)

您也可以使用带有Proxy类的重载Jsoup#proxy.

在Jsoup 1.9.1之前:(详细方法)

// Setup proxy
Proxy proxy = new Proxy(                                      //
        Proxy.Type.HTTP,                                      //
        InetSocketAddress.createUnresolved("127.0.0.1", 8080) //
);

// Fetch url with proxy
Document doc = Jsoup //
               .connect("http://www.example.com/") //
               .proxy(proxy) //
               .userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2") //
               .header("Content-Language", "en-US") //
               .get();
Run Code Online (Sandbox Code Playgroud)

参考文献:


Rya*_*yan 33

您不必通过Jsoup获取网页数据.这是我的解决方案,但它可能不是最好的.

  URL url = new URL("http://www.example.com/");
  Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); // or whatever your proxy is
  HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);

  uc.connect();

    String line = null;
    StringBuffer tmp = new StringBuffer();
    BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    while ((line = in.readLine()) != null) {
      tmp.append(line);
    }

    Document doc = Jsoup.parse(String.valueOf(tmp));
Run Code Online (Sandbox Code Playgroud)

它就是.这通过代理获取html页面的源,然后使用Jsoup解析它.


Ale*_*arc 5

System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "1080");
Document doc = Jsoup.connect("www.google.com").get();
Run Code Online (Sandbox Code Playgroud)

这是错误的解决方案,因为解析通常是多线程的,并且我们通常需要更改代理。此代码仅为所有线程设置一个代理。因此最好不要使用Jsoup.Connection。


bit*_*ter 5

您可能想在运行程序之前添加它

final String authUser = "USERNAME";
final String authPassword = "PASSWORD";



Authenticator.setDefault(
               new Authenticator() {
                  public PasswordAuthentication getPasswordAuthentication() {
                     return new PasswordAuthentication(
                           authUser, authPassword.toCharArray());
                  }
               }
            );

..

System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "1080");
..
Run Code Online (Sandbox Code Playgroud)