网络爬虫ip代理服务器【程序样例】

本文介绍了当爬虫遭遇IP被封禁时的解决方法,包括如何利用代理IP进行动态轮询,以及推荐了第三方代理平台如crawlera。文中还提供了一个简单的Java代码示例,演示了如何设置HTTP代理来获取网页内容。

爬虫有的时候会遇到被禁ip的情况,这个时候你可以找一下代理网站,抓取一下ip,来进行动态的轮询就没问题了,也可以用别人做好的第三方ip代理平台,比如说crawlera,crawlera是一个利用代理IP地址池来做分布式下载的第三方平台。【具体介绍请看这篇博客:http://blog.youkuaiyun.com/djd1234567/article/details/51741557

package daili;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
/*
 * author:合肥工业大学 管院学院 钱洋 
 *1563178220@qq.com
 *博客地址:http://blog.youkuaiyun.com/qy20115549/
*/
public class GetHtml {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //输入代理ip,端口,及所要爬取的url
        gethtml("183.136.217.74",8080,"http://club.autohome.com.cn/bbs/forum-c-2533-1.html?orderby=dateline&qaType=-1");

    }
    public static String gethtml(String ip,int port,String url) throws UnsupportedEncodingException{
        URL url1 = null;
        try {
            url1 = new URL(url);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        InetSocketAddress addr = null;
        //代理服务器的ip及端口
        addr = new InetSocketAddress(ip, port);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy
        InputStream in = null;
        try {
            URLConnection conn = url1.openConnection(proxy);
            conn.setConnectTimeout(3000);
            in = conn.getInputStream();
        } catch (Exception e) {
            System.out.println("ip " + " is not aviable");//异常IP
        }

        String s = convertStreamToString(in);
        System.out.println(s);
        return s;

    }
    public static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
        if (is == null)
            return "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"gb2312"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();

    }
}

如下图,便可以抓取到url对应的html内容。

这里写图片描述

基本原理: 代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过URL对象建立输入流读取相应的网页内容,最后按照web服务器的工作方式将网页内容发送给用户浏览器 源程序: import java.net.*; import java.io.*; public class MyProxyServer { public static void main(String args[]) { try { ServerSocket ss=new ServerSocket(8080); System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } } }; class process implements Runnable { Socket s; public process(Socket s1) { s=s1; } public void run() { String content=" "; try { PrintStream out=new PrintStream(s.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); String info=in.readLine(); System.out.println("now got "+info); int sp1=info.indexOf(' '); int sp2=info.indexOf(' ',sp1+1); String gotourl=info.substring(sp1,sp2); System.out.println("now connecting "+gotourl); URL con=new URL(gotourl); InputStream gotoin=con.openStream(); int n=gotoin.available(); byte buf[]=new byte[1024]; out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); out.println("Content_Length:"+n); out.println(" "); while ((n=gotoin.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); } catch (IOException e) { System.out.println("Exception:"+e); } } };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值