Jsoup使用代理ip爬虫
2017-01-19 16:52
656 查看
Jsoup本身没有[b]设置代理ip的功能,但是可以通过httpsUrlConnection设置代理ip获取页面内容,[/b]
然后用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();
}
}
然后用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();
}
}
相关文章推荐
- 使用python为爬虫获取免费代理ip
- python3实现网络爬虫(7)-- 使用ip代理抓取网页
- [Python爬虫] 使用urllib2库检测代理ip是否可用,并使用代理提交GET、POST请求
- Python 爬虫入门(二)—— IP代理使用
- Python3 爬虫使用User Agent和代理IP隐藏身份
- 鱼c笔记——Python爬虫(三):使用User Agent和代理IP隐藏身份
- Python 爬虫入门—— IP代理使用
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- Python 爬虫入门(二)—— IP代理使用 - 不剃头的一休哥 - 博客园
- Python3网络爬虫之使用User Agent和代理IP隐藏身份
- 使用阻塞队列爬取代理ip实现爬虫
- python3使用代理ip伪装爬虫访问网站
- 如何使用ip代理爬虫
- Python 爬虫入门(二)—— IP代理使用
- 爬虫使用代理ip
- Python 爬虫之使用代理ip
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- 网络爬虫01: Urllib2库使用代理IP
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- java爬虫(使用jsoup设置代理,抓取网页内容)