浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

    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);
       
        }
    }

posted on 2012-02-04 14:56  lexus  阅读(260)  评论(0编辑  收藏  举报