• HOWTO use httpcomponet example


        private void promotion_price_situation_four(Extraction extraction,
                String html, CrawlResult product) throws IOException,
                ClientProtocolException {
            String http_response=null;
            //进行ajax http请求促俏价格 situation four
            //http://detail.tmall.com/item.htm?id=2223769663&is_b=1&spm=1008.1000032.1000012.4
            String ajax_request_url = null;
            try {
                Pattern regex = Pattern.compile("<textarea id=\"J_VipPriceData\"[\\s\\S]*?shopVipUrl[\\s\\S]*?\"([\\s\\S]*?)\",[\\s\\S]*?</textarea>");
                Matcher regexMatcher = regex.matcher(html);
                if (regexMatcher.find()) {
                    ajax_request_url = regexMatcher.group(1);
                }
            } catch (PatternSyntaxException ex) {
                // Syntax error in the regular expression
                logger.warn(ex.toString());
            }
            logger.debug("ajax_request_url");
            logger.debug(ajax_request_url);
            if(ajax_request_url!=null){
                ajax_request_url=ajax_request_url.trim();               

                 HttpClient httpclient = new DefaultHttpClient();
                try {
    //                HttpGet httpget = new HttpGet("http://tbskip.taobao.com/json/promotion_info_list.htm?price=45.80&seller_id=105152347&buyer_id=0");
                    String s="http://tbskip.taobao.com/json/promotion_info_list.htm?price=219.00&seller_id=728292607&buyer_id=0";
                    logger.debug("====");
                    logger.debug(ajax_request_url);
                    HttpGet httpget = new HttpGet(ajax_request_url);
                    HttpParams params = new SyncBasicHttpParams();
                    params
                        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
                        .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
                        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
                        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
                        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
                        .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");
                    httpget.setParams(params);
                    System.out.println("executing request " + httpget.getURI());

                    // Create a response handler
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                   
                    String responseBody = httpclient.execute(httpget, responseHandler);
                    System.out.println("----------------------------------------");
                    System.out.println(responseBody);
                    System.out.println("----------------------------------------");
                    http_response=responseBody;
                }catch(Exception ex)
                {
                    logger.warn(ex.toString());
                }
                finally {
                    // When HttpClient instance is no longer needed,
                    // shut down the connection manager to ensure
                    // immediate deallocation of all system resources
                    httpclient.getConnectionManager().shutdown();
                }
                if(http_response.trim().endsWith(";")==false)
                {
                    http_response=http_response.trim()+";";
                }
                logger.debug(http_response.trim());

                String promotion_price = null;
                String json_string="var shopVipData={\"msg\":\"\",promo:[{\"type\":\"普通会员\",\"price\":\"45.75\"},{\"type\":\"高级会员\",\"price\":\"45.71\"},{\"type\":\"VIP会员\",\"price\":\"45.34\"},{\"type\":\"至尊VIP\",\"price\":\"44.88\"}]};";
                json_string=http_response;
                logger.debug(json_string);
                String js_string="var price;for(var i=0;i<=shopVipData.promo.length-1;i++){if(shopVipData.promo[i].type=='普通会员'){price=shopVipData.promo[i].price;}}";

                ScriptEngineManager manager = new ScriptEngineManager();
                ScriptEngine engine = manager.getEngineByName("JavaScript");
                Compilable compilable = (Compilable) engine;
                CompiledScript script = null;
                Bindings binds = engine.createBindings();
                try {
                    script = compilable.compile(json_string+js_string);
                    script.eval(binds);
                    promotion_price=binds.get("price").toString();
                    logger.debug("=-----------------------------");
                   
                } catch (ScriptException e) {
                    e.printStackTrace();
                }   
                this.is_add_promotion_price(extraction, product, promotion_price);
           
            }
        }

  • 相关阅读:
    mysql学习笔记(七)
    Mysql学习笔记(八)
    vcpkg安装库
    nvm node版本管理工具
    Node.js版本管理工具nvm
    mysql安装后启动及navicat绿色版
    蚁景Web安全12期笔记
    python协程asyncio的个人理解
    EF或原生sql语句使用全文索引
    在拥挤和变化的世界中茁壮成长:C++ 2006–2020
  • 原文地址:https://www.cnblogs.com/lexus/p/2337998.html
Copyright © 2020-2023  润新知