美文网首页
关于代理ip的一些笔记

关于代理ip的一些笔记

作者: silencefun | 来源:发表于2019-01-22 18:19 被阅读20次

还是爬虫需要ip池支撑。
搜一下是一大堆免费的但是需要过滤筛选 能用的。

1.免费代理ip的获取

https://www.xicidaili.com/nn/

image.png

http://www.66ip.cn/nmtq.php?getnum=1

image.png

第一个 需要解析
第二个 可以自定义数量

2.验证

看能否使用正常访问

 /**
 * 测试 代理ip是否有效
 * 
 * @param ip
 * @param port
 */
public static void createIPAddress(String ip, int port) {
    URL url = null;
    try {
        url = new URL("http://www.baidu.com");
    } catch (MalformedURLException e) {
        System.out.println("url invalidate");
    }
    InetSocketAddress addr = null;
    addr = new InetSocketAddress(ip, port);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy
    InputStream in = null;
    try {
        URLConnection conn = url.openConnection(proxy);
        conn.setConnectTimeout(1000);
        in = conn.getInputStream();
    } catch (Exception e) {
         e.printStackTrace();
         System.err.println("ip " + ip + " is not aviable");// 异常IP
    }
        String s = convertStreamToString(in);

    if (s.indexOf("baidu") > 0) {// 有效IP
        System.err.println(ip + ":" + port + " is ok");
        
        
        CrawlerUtis.appendLog("C:\\Users\\21555\\Desktop\\ip_enable.txt",
                ip + " " + port + "\r\n");
    }
}


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

}

3.关于解析

3.1xicidail网站的解析

 public static List<String> AnalyIppool() {

    try {
        URL url = new URL("https://www.xicidaili.com/nn/");
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible, MSIE 7.0, Windows NT 5.1, TencentTraveler 4.0)");
                    //要加上User-Agent
               connection.setRequestProperty("Charsert", "UTF-8"); //设置请求编码
         
               connection.setRequestProperty("Content-Type",  "application/json"); 
                connection.connect();
        InputStream in = connection.getInputStream();
 
        
        
        Document document = Jsoup.parse(convertStreamToString(in));

        Elements ss = document.getElementsByClass("odd");
        for (Element element : ss) {
            
            AnalyIpAndcheck(element.text());
            //System.out.println(element.text());
        }

        
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

private static String AnalyIpAndcheck(String iporign) {
    String[] ipp=iporign.split(" ");
    createIPAddress(ipp[0],Integer.parseInt(ipp[1]));
    
    
    return null;
}

3.2 第二个直接是接口数据
http://www.66ip.cn/nmtq.php?getnum=2000
请求多个ip,每次读一行,然后可以使用线程池来执行。

关键代码:

    private static ThreadPoolExecutor executor== new ThreadPoolExecutor(5, 30, 300, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(3),
                new ThreadPoolExecutor.CallerRunsPolicy());

传入解析后的list

public static List<String> AnalyIppool2(String path) {

    List<String> list = CrawlerUtis.filter(path);//转化为list 方法 通过本地文件读 可以直接写请求
    
    for (String string : list) {
        
        
        executor.execute(new Runnable() {
            
            @Override
            public void run() {
                String[] ip=string.split(":");
                createIPAddress(ip[0],Integer.parseInt(ip[1]));
                
            }
        });
    
        
    }

相关文章

  • 关于代理ip的一些笔记

    还是爬虫需要ip池支撑。搜一下是一大堆免费的但是需要过滤筛选 能用的。 1.免费代理ip的获取 https://w...

  • ip代理中的动静态ip以及独享、共享ip

    有接触过代理IP的用户可能对于代理ip的一些分类有所了解,像动态ip、静态ip、独享ip、共享ip这些分类,都是i...

  • 免费的换ip工具弊端

    从价格来看,代理ip有免费以及付费两种,关于免费代理ip的使用,狮子ip并不建议大家长期使用。 免费IP的提取还是...

  • 关于代理IP的几个常见问题

    前几天在知乎浏览爬虫代理ip的时候看到有人问: 关于ip代理的几个问题不解,求解答: 1、代理商靠什么提供代理服务...

  • python3的爬虫笔记5——代理IP和时间设置、异常处理

    对于反爬虫机制的处理,除了笔记2中伪造浏览器的方法,还可以使用代理IP和时间设置 一、代理IP 适用情况:限制IP...

  • 爬虫代理IP池的实现

    代理池的思路 之前写过一篇关于代理的文档:爬虫和IP代理,里面介绍了一些代理的基本知识,后半部分我也见到那说了下如...

  • 爬虫代理小记与aiohttp代理尝试

    总结了一些爬虫代理的资料和知识,并尝试使用asyncio和aiohttp使用代理ip访问目标网站,按代理IP的访问...

  • 这就是我的爬虫基本功!使用代理 IP和绕过反爬机制!

    使用代理 IP 之前我了解到的使用代理 IP 的方法,一般都是提前从一些免费的代理网站上爬取免费代理,存在本地或者...

  • 2018-09-25

    代理ip常用的一些功能。 1:突破自身ip访问限制,现在有许多的网站都对ip地址访问进行了限制,这时则可以通过代理...

  • aiohttp与requests效率对比

    之前使用scrapy爬取了一些代理网站的代理,因为像知乎等网站针对ip进行了反爬,如果限制了ip地址,需要使用代理...

网友评论

      本文标题:关于代理ip的一些笔记

      本文链接:https://www.haomeiwen.com/subject/cujrjqtx.html