Ejemplo n.º 1
0
 public static void main(String[] args) throws Exception {
   ProxyUtli.setProxy(true);
   DrugstorePage drugstorePage = new DrugstorePage();
   drugstorePage.url =
       "http://www.drugstore.com/cottonelle-fresh-care-flushable-moist-wipes-tub/qxp157237?catid=180658";
   ParserDrugstorePage parserDrugstorePage = new ParserDrugstorePage(drugstorePage);
   parserDrugstorePage.checkprice();
   System.out.println(drugstorePage);
 }
Ejemplo n.º 2
0
  public void checkprice() throws Exception {
    // System.out.println("checking drugstore url:" + page.getUrl());

    String cookies = ""; // DrugstoreLogin.getCookies();

    URL url = new URL(page.getUrl());
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setConnectTimeout(Constant.connect_timeout);
    urlConnection.setRequestProperty(
        "User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT)");
    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    urlConnection.setRequestProperty("Cookie", cookies);

    urlConnection.connect();
    // InputStream is = urlConnection.getInputStream();
    //
    // BufferedReader reader = new BufferedReader(new
    // InputStreamReader(is));
    //
    // String s;
    // StringBuilder result = new StringBuilder();
    // while (((s = reader.readLine()) != null)) {
    // result.append(s);
    // }
    //
    // System.out.println("result= " + result.toString());
    //
    // is.close();

    Parser parser = new Parser(urlConnection);

    parser.setEncoding(Constant.ENCODE);

    NodeFilter name_filter3 =
        new AndFilter(
            new NodeClassFilter(Div.class), new HasAttributeFilter("id", "divAvailablity"));
    NodeFilter name_filter4 =
        new AndFilter(new NodeClassFilter(Div.class), new HasAttributeFilter("id", "productprice"));

    OrFilter lastFilter = new OrFilter();
    lastFilter.setPredicates(
        new NodeFilter[] {new NodeClassFilter(TitleTag.class), name_filter3, name_filter4});

    NodeList list = parser.extractAllNodesThatMatch(lastFilter);
    for (int i = 0; i < list.size(); i++) {
      Node tag = list.elementAt(i);
      if (tag instanceof TitleTag) {
        TitleTag d = (TitleTag) tag;
        page.title = d.getTitle().replaceAll("drugstore.com", "").replaceAll("\\|", "").trim();
      } else if (tag instanceof Div) {
        Div d = (Div) tag;
        String sStr = d.getStringText();
        // System.out.println(sStr);
        if ("productprice".equalsIgnoreCase(d.getAttribute("id"))) {
          page.price = getPrice(sStr);
        } else if ("divAvailablity".equalsIgnoreCase(d.getAttribute("id"))) {
          if (sStr.indexOf("In Stock") >= 0 || sStr.indexOf("in stock") >= 0) {
            page.instock = true;
          }
        }
        // System.out.println(d.getStringText());
        // getinStock(d);
      }
    }
  }