Jsoup使用代理ip爬虫

时间:2022-04-20 16:57:09

Jsoup本身没有设置代理ip的功能,但是可以通过httpsUrlConnection设置代理ip获取页面内容,

然后用jsoup转为Document结构,代码如下:


public void getDocByJsoup(String href){

String ip = "221.237.155.64";

int port = 9797;

try {

Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip, port));

 

URL url = new URL(href);  

HttpsURLConnection urlcon = (HttpsURLConnection)url.openConnection(proxy);  

urlcon.connect();         //获取连接  

InputStream is = urlcon.getInputStream();  

BufferedReader buffer = new BufferedReader(new InputStreamReader(is));  

StringBuffer bs = new StringBuffer();  

String l = null;  

while((l=buffer.readLine())!=null){  

bs.append(l);  

}  

System.out.println(bs.toString());  

Document doc = Jsoup.parse(bs.toString());

} catch (Exception e) {

e.printStackTrace();

}

}