新闻  |   论坛  |   博客  |   在线研讨会
python、java爬虫使用代理的区别
laical | 2020-11-09 17:15:46    阅读:159   发布文章

大数据时代下网络爬虫也越来越多,爬虫目前主要开发语言为java、Python、c++  对于一般的信息采集需要,各种语言差别不是很大,但是大多数爬虫用户都会选择python和java开发语言。

python 爬虫网络功能强大,支持模拟登陆,python写起程序来真的很便捷

java爬虫的解析功能非常好

无论是java还是python,基本上爬虫业务需求都满足,具体看个人爬虫业务需求,选择适合自己的爬虫语言。

两者之间使用爬虫代理的区别:

python爬虫所选框架scrapy:

        class ProxyMiddleware(object):                
            def process_request(self, request, spider):
                # 代理服务器(产品官网 www.16yun.cn)
                proxyHost = "t.16yun.cn"
                proxyPort = "31111"

                # 代理验证信息
                proxyUser = "username"
                proxyPass = "password"

                request.meta['proxy'] = "http://{0}:{1}".format(proxyHost,proxyPort)

                # 添加验证头
                encoded_user_pass = base64ify(proxyUser + ":" + proxyPass)
                request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass                    

                # 设置IP切换头(根据需求)
                tunnel = random.randint(1,10000)
                request.headers['Proxy-Tunnel'] = str(tunnel)

java爬虫所选框架jsoup:

{
    // 代理验证信息
    final static String ProxyUser = "username";
    final static String ProxyPass = "password";

    // 代理服务器(产品官网 www.16yun.cn)
    final static String ProxyHost = "t.16yun.cn";
    final static Integer ProxyPort = 31111;

    // 设置IP切换头
    final static String ProxyHeadKey = "Proxy-Tunnel";


    public static String getUrlProxyContent(String url)
    {
        Authenticator.setDefault(new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray());
            }
        });
        // 设置Proxy-Tunnel
        Random random = new Random();
        int tunnel = random.nextInt(10000);
        String ProxyHeadVal = String.valueOf(tunnel);

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ProxyHost, ProxyPort));

        try
        {
            // 处理异常、其他参数
            Document doc = Jsoup.connect(url).timeout(3000).header(ProxyHeadKey, ProxyHeadVal).proxy(proxy).get();

两种爬虫语言使用代理的代码demo其实是差不多的,由于是不同的语言和框架所以爬虫程序执行的命令是不一样,需要自己去分析解决。

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客