Example usage for org.jsoup.nodes Element getElementsByTag

List of usage examples for org.jsoup.nodes Element getElementsByTag

Introduction

In this page you can find the example usage for org.jsoup.nodes Element getElementsByTag.

Prototype

public Elements getElementsByTag(String tagName) 

Source Link

Document

Finds elements, including and recursively under this element, with the specified tag name.

Usage

From source file:fr.arlefebvre.pronostics.controller.EuroMatchListController.java

@RequestMapping("/euro2016/matches")
public List<Match> matches() {
    if (pseudoCache != null && !pseudoCache.isEmpty())
        return pseudoCache;
    ArrayList<Match> result = new ArrayList<Match>();
    String uri = "http://www.lequipe.fr/Football/Euro/Saison-2016/calendrier-resultats.html";

    //On se connecte au site et on charge le document html

    Document doc;/*  www  .  j av a 2s. c  o  m*/
    try {
        doc = Jsoup.connect(uri).get();

        Elements elements = doc.getElementsByClass("mainDate");
        for (Element element : elements) {
            Element title = element.getElementsByClass("title").first();
            String date = title.text();

            Element tbody = element.getElementsByTag("tbody").first();
            for (Element matchElement : tbody.children()) {
                String groupe = matchElement.getElementsByClass("date").first().text();
                String home = matchElement.getElementsByClass("domicile").first().text();
                String away = matchElement.getElementsByClass("exterieur").first().text();

                Match m = new Match();
                m.setDate(date);
                m.setHomeTeamId(home);
                m.setAwayTeamId(away);
                m.setGroup(groupe);
                result.add(m);
            }
        }

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

    if (pseudoCache == null)
        pseudoCache = result;
    return result;
}

From source file:eu.masconsult.bgbanking.banks.fibank.my.MyFIBankClient.java

private RawBankAccount obtainBankAccountFromHtmlTableRow(Element row) {
    Elements cells = row.getElementsByTag("td");
    if (cells == null || cells.size() == 0) {
        return null;
    }//from   ww w  .ja  v a  2s .co m

    if (cells.size() == 1) {
        return null;
    }

    String iban = extractIBAN(cells.get(0).text());
    return new RawBankAccount().setServerId(iban).setIBAN(iban).setName(Convert.formatIBAN(iban))
            .setCurrency(cells.get(1).text()).setAvailableBalance(Convert.strToFloat(cells.get(2).text()))
            .setBalance(Convert.strToFloat(cells.get(2).text()));
}

From source file:es.logongas.util.seguridad.AuthenticationProviderImplMoodle.java

@Override
public Principal authenticate(Credential credential) {
    try {/*from w  ww  .j  a va 2s  .c  om*/
        StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();

        if ((credential instanceof CredentialImplLoginPassword) == false) {
            return null;
        }

        CredentialImplLoginPassword credentialImplLoginPassword = (CredentialImplLoginPassword) credential;

        if (loginAdmin.equalsIgnoreCase(credentialImplLoginPassword.getLogin())) {

            if (passwordEncryptor.checkPassword(credentialImplLoginPassword.getPassword(),
                    passwordAdmin) == false) {
                return null;
            }

        } else {

            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            DefaultHttpClient httpClientPost = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(moodleLoginURL);
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("username", credentialImplLoginPassword.getLogin()));
            nvps.add(new BasicNameValuePair("password", credentialImplLoginPassword.getPassword()));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            HttpResponse response1 = httpClientPost.execute(httpPost);
            InputStream inputStream = response1.getEntity().getContent();
            Document document = Jsoup.parse(inputStreamToString(inputStream));

            Elements divElements = document.getElementsByClass("logininfo");
            if (divElements.size() == 0) {
                return null;
            }
            Element divElement = divElements.get(0);
            Elements aElements = divElement.getElementsByTag("a");
            if (aElements.size() == 0) {
                return null;
            }
            Element aElement = aElements.get(aElements.size() - 1);
            if (aElement.attr("href").indexOf("logout") < 0) {
                return null;
            }

            DefaultHttpClient httpclient2 = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(aElement.attr("href"));

            httpclient2.execute(httpGet);

        }
        GenericDAO<Identity, Integer> genericDAO = daoFactory.getDAO(Identity.class);
        Identity identity = genericDAO.readByNaturalKey(credentialImplLoginPassword.getLogin());

        return identity;

    } catch (BusinessException ex) {
        return null;
    } catch (Exception ex) {
        log.info("Fallo al conectarse al moodle", ex);
        throw new RuntimeException(ex);
    }
}

From source file:de.fzi.ALERT.actor.MessageObserver.NotificationObserver.JMSNotificationParser.java

public Message parseSimilarIssues(Document doc) {
    Message message = null;//from   w w w  . ja v  a  2 s  . co m
    try {

        Elements pID = doc.select("p|patternId");
        String patternId = pID.get(0).text();
        // String message = doc.select(query);
        Pattern pattern;
        pattern = patternDAO.findById(patternId);

        String content = "Similar Issues\n";

        if (pattern != null) {
            message = new Message();
            message.setPatternId(pattern);

            Elements items = doc.select("item");
            for (org.jsoup.nodes.Element item : items) {
                String subject = item.getElementsByTag("subject").text();
                String issueurl = item.getElementsByTag("url").text();
                String similarity = item.getElementsByTag("similarity").text();
                content = content + subject + "\n" + issueurl + "\n" + similarity + "\n";
            }
            message.setSubject("Similar Issues");
            message.setSummary("default summary");
            message.setContent(content);
            message.setMsgDate(new Date());
            message.setMsgID(1);
        } else
            System.out.println("can't find patternID of the complex event:" + patternId);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return message;
}

From source file:com.shahnami.fetch.Controller.FetchMovies.java

public List<Movie> getBollyMovies(String query) {
    try {//  www.j a  v  a2  s  .  co m
        Document doc;
        Elements searchDetails;
        String link;
        String title;
        String image = null;
        Elements linkAndTitles;
        Document movieDetails;
        double rating = 0;
        String magnetLink;
        String torrentFile;
        NumberFormat formatter;
        String output;
        if (query.equalsIgnoreCase("")) {
            //
        } else {
            doc = Jsoup.connect("https://1337x.to/search/" + URLEncoder.encode(query, "UTF-8") + "+hindi/1/")
                    .userAgent("Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803").get();
            searchDetails = doc.getElementsByClass("coll-1");
            for (Element e : searchDetails) {
                linkAndTitles = e.getElementsByTag("strong");
                for (Element e1 : linkAndTitles) {
                    link = "https://1337x.to" + e1.getElementsByTag("a").first().attr("href");
                    title = e1.getElementsByTag("a").first().html();
                    if (!link.contains("/mirror")) {
                        Movie m = new Movie();
                        m.setTitle(title.replace("<b>", "").replace("</b>", "").trim()); //.substring(0, 47)+ "..."
                        m.setLanguage("Hindi");
                        m.setUrl(link);
                        Pattern pattern = Pattern.compile(".*([\\s(]+[0-9]{4}[\\s)]+).*");
                        Matcher matcher = pattern.matcher(title);
                        while (matcher.find()) {
                            m.setYear(Integer
                                    .parseInt(matcher.group(1).replace("(", "").replace(")", "").trim()));
                        }
                        movieDetails = Jsoup.connect(link).get();
                        try {
                            image = movieDetails.getElementsByClass("moive-box").first().getElementsByTag("img")
                                    .first().attr("src");
                            rating = Float.parseFloat(
                                    movieDetails.getElementsByClass("rateing").first().getElementsByTag("i")
                                            .attr("style").split(":")[1].replace("%;", "").trim());
                        } catch (Exception ex) {
                            //
                        }
                        magnetLink = movieDetails.getElementsByClass("magnet").first().attr("href");
                        torrentFile = movieDetails.getElementsByClass("torrent").first().attr("href");
                        formatter = NumberFormat.getNumberInstance();
                        formatter.setMinimumFractionDigits(2);
                        formatter.setMaximumFractionDigits(2);
                        output = formatter.format(rating / 10);
                        rating = Double.parseDouble(output);
                        if (rating < 1) {
                            rating = 0;
                        }
                        m.setRating(rating);
                        m.setSmall_cover_image(image);
                        List<Torrent> torrents = new ArrayList<>();
                        Torrent t = new Torrent();
                        t.setUrl(magnetLink);
                        Torrent t2 = new Torrent();
                        t2.setUrl(torrentFile);
                        torrents.add(t);
                        torrents.add(t2);
                        m.setTorrents(torrents);
                        m.getTorrents().get(0).setSeeds(
                                Integer.valueOf(movieDetails.getElementsByClass("green").first().text()));
                        m.getTorrents().get(0).setPeers(
                                Integer.valueOf(movieDetails.getElementsByClass("red").first().text()));
                        m.setIsBollywood(true);
                        m.setSize(movieDetails.getElementsByClass("list").first().getElementsByTag("li").get(3)
                                .text().substring(10).trim());
                        _movies.add(m);
                    }
                }
                //String link = linkAndTitle.getElementsByAttribute("href").first().text();
                //System.out.println(link);
                //String title = linkAndTitle.getElementsByTag("b").text();
                //System.out.println(title);
            }
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(FetchMovies.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FetchMovies.class.getName()).log(Level.SEVERE, null, ex);
    }
    return _movies;
}

From source file:mx.itdurango.rober.siitdocentes.asynctasks.GruposTask.java

/**
 * Procesa el html resultante de la peticin del listado de grupos descomponiendolo y asignandolo a un ArrayList
 *
 * @param html cuerpo html del resultado de la peticin
 *///from w  w  w  .j av  a2  s  .com
public void procesa(String html) {
    //se genera un documento donde se almacena el contenido html listo para ser procesado.
    Document doc = Jsoup.parse(html);
    //se obtiene la tabla donde se encuentra el contenido que interesa
    Element tabla = doc.getElementsByTag("table").get(0);
    //se obtienen todos los renglones de la tabla
    Elements renglones = tabla.getElementsByTag("tr");
    //arraylist que almacenar la informacin de los grupos
    ArrayList<Grupos> gcs = new ArrayList<Grupos>();
    //se recorre cada renglon almacenandolo en un objeto
    for (Element tr : renglones) {
        //se obtienen todos los elementos td de cada renglon.
        Elements tds = tr.getElementsByTag("td");
        //lleva el control de la columna que se est evaluando
        int l = 1;
        //objeto para lmacenar la informacion de cada grupo
        Grupos gc = new Grupos();
        //se recorren todos los elementos td del renglon actual
        for (Element td : tds) {
            //en el renglon 1 se encuentra la informacion del grupo con el siguiente formato
            //<b> CLAVE_MATERIA  </b> <br> NOMBRE DE LA MATERIA
            if (l == 1) {
                //se obtiene el contenido de la celda
                String datos = td.html();
                //eliminar las etiquetas de inicio de negritas
                datos = datos.replaceAll("<b>", "");
                //separar la cadena para tener en la posicin 0 la clave de la materia y en la posicion 1 el nombre de la misma.
                String m[] = datos.split("</b> <br />");
                gc.setClave(m[0]); //se asigna la clave de la materia al campo correspondiente
                gc.setNombre(m[1]);//se asigna el nombre de la materia al campo correspondiente
            } else if (l == 2) { //en la columna 2 se encuentra el grupo
                gc.setGrupo(td.html());
            } else if (l == 3) { //en la columna 3 se encuentra el numero de alumnos inscritos
                gc.setAlumnos(td.html());
            } else if (l == 4) { //en la columna 4 se encuentran los vinculos para asignar calificaciones parciales con el siguiente formato
                // <img src="http://siit.itdurango.edu.mx/img/iconos/captura_calif.gif"
                // onclick="window.location = &quot;calificaciones_parciales.php?periodo=20141&amp;materia=IT8851&amp;grupo=8TA&quot;"
                // alt="Captura de Informacin" style="cursor:pointer">

                //tomamos el contenido de la celda
                String params = td.html();
                //si separamos mediante la cadena "&quot;" podemos obtener solamente la url con parmetros que se tiene que mandar llamar
                String separado[] = params.split("&quot;");
                params = separado[1]; // solo los parametros
                params = params.replaceAll("&amp;", "&");
                //asignar la url a su campo correspondiente
                gc.setUrl(params);
            }
            //incrementar el numero de columna
            l++;
        }
        //si la clave es nula significa que no es una materia, probablemente sea el encabezado de la tabla
        if (gc.getClave() != null)
            gcs.add(gc);
    }
    //se genera un adapter nuevo con la informacin obtenida para ser asignado al listview de grupos.
    context.lvGrupos.setAdapter(new GruposAdapter(context, R.layout.item_grupos, gcs));
}

From source file:am.roadpolice.roadpolice.downloaders.Submitter.java

/**
 * This function process URL and collect needed information about
 * violation and add it to Violation List (mViolationInfoList).
 *
 * @param url URL from which data will be processed.
 * @return null if no error occurs; otherwise ERROR1/ERROR2 if something
 * was changed in server behaviour,  ERROR3 if error occurs while trying
 * to get JSOUP document, or server error text.
 *//* w w w . j a va2  s.  c  o  m*/
private String processUrl(final String url) {
    Logger.debugLine();
    Logger.debug(TAG, "Processing URL: " + url);

    HttpGet httpGet = new HttpGet(url);
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECT_TIMEOUT);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

    try {

        HttpResponse response = httpClient.execute(httpGet);
        java.util.Scanner s = new java.util.Scanner(response.getEntity().getContent()).useDelimiter("\\A");

        Document document = Jsoup.parse(s.hasNext() ? s.next() : null);
        // In the case if  some data  was provided not correct in
        // the url server generates page with red text, we handle
        // this situation and return text in the red block.
        Elements errorElement = document.getElementsByClass("red");
        if (errorElement != null && errorElement.first() != null) {
            final String errorText = errorElement.first().text();
            Logger.debug(TAG, "Found Error Element (RED): " + errorText);
            return errorText;
        }

        Elements tableElements = document.getElementsByClass("dahk_yes");
        tableElements.addAll(document.getElementsByClass("dahk_no"));
        for (Element element : tableElements) {
            Elements tdElements = element.getElementsByTag("td");

            int tdElementsCount = ViolationInfo.COL_OWNER_FULL_NAME;
            ViolationInfo violationInfo = null;
            for (Element tdElement : tdElements) {
                final String text = tdElement.text().trim();

                // We found vehicle registration number.
                if (text.equalsIgnoreCase(mRegNum)) {
                    // Create new class object to store data.
                    violationInfo = new ViolationInfo(mRegNum);
                    violationInfo.setCertificateNumber(mCerNum);
                    continue;
                }

                // Violation Info object was not created, reason can be
                // that something is changed on the server side.
                if (violationInfo == null) {
                    return ERROR_ON_SERVER_SIDE;
                }

                switch (tdElementsCount) {
                case ViolationInfo.COL_OWNER_FULL_NAME:
                    violationInfo.setOwnerFullName(text);
                    break;

                case ViolationInfo.COL_OWNER_ADDRESS:
                    violationInfo.setOwnerAddress(text);
                    break;

                case ViolationInfo.COL_TO_PAY:
                    violationInfo.setToPay(text);
                    break;

                case ViolationInfo.COL_PAYED:
                    violationInfo.setPayed(text);
                    break;

                case ViolationInfo.COL_CAR_MODEL:
                    violationInfo.setCarModel(text);
                    break;

                case ViolationInfo.COL_THE_DECISION:
                    // Do Nothing ...
                    break;

                case ViolationInfo.COL_DATE:
                    violationInfo.setDate(text);
                    break;

                case ViolationInfo.COL_PIN:
                    violationInfo.setPin(text);
                    break;

                default:
                    return ERROR_WHILE_PARSING_DATA;
                }

                tdElementsCount++;
            }

            // Add items to the list.
            mViolationInfoList.add(violationInfo);
        }
    } catch (IOException e) {
        Logger.error(TAG, "----> Exception occurs while trying to get JSOUP document.");
        Logger.error(TAG, "----> Message: " + e.getMessage());
        return ERROR_WHILE_CREATING_JSOUP;
    }

    return null;
}

From source file:com.gote.downloader.kgs.KGSDownloader.java

/**
 * Check if a game is public, if yes, then the URL of that game will be sent back.
 * /*from  w  w  w.  j  a  v a  2 s . co m*/
 * @param pCell Element which represents the first KGS archives column
 * @return link of the SGF or null
 */
public String isPublicGame(Element pCell) {
    Elements a = pCell.getElementsByTag("a");

    if (a != null && a.size() > 0) {
        // Check if it is a visible game
        if (a.html().equals(KGSUtils.KGS_TAG_FR_YES)) {
            return a.attr("href");
        }
    }

    return null;
}

From source file:eu.masconsult.bgbanking.banks.sgexpress.SGExpressClient.java

@Override
public List<RawBankAccount> getBankAccounts(String authTokenString)
        throws IOException, ParseException, AuthenticationException {
    AuthToken authToken = AuthToken.fromJson(authTokenString);

    String response = loadPageWithAuth(getHttpClient(), authToken, LIST_ACCOUNTS_XML_ID);

    Document doc = Jsoup.parse(response, BASE_URL);

    Element content = doc.getElementById("main");
    if (content == null) {
        throw new ParseException("getBankAccounts: can't find #main");
    }/*ww  w  . j  a  v a 2  s.  co m*/

    Elements tables = content.select("section.result table.data");
    if (tables == null || tables.size() == 0) {
        throw new ParseException("getBankAccounts: can't find table section.result table.data");
    }

    Elements rows = tables.first().getElementsByTag("tr");
    if (rows == null || rows.size() == 0) {
        throw new ParseException("getBankAccounts: first table is empty");
    }

    ArrayList<RawBankAccount> bankAccounts = new ArrayList<RawBankAccount>(rows.size());

    String type = "undef";
    for (Element row : rows) {
        if (row.getElementsByTag("th").size() > 0) {
            // header row
            type = row.child(0).text();
        } else {
            RawBankAccount bankAccount = obtainBankAccountFromHtmlTableRow(type, row);
            if (bankAccount != null) {
                bankAccounts.add(bankAccount);
            }
        }
    }

    return bankAccounts;
}

From source file:nl.phanos.liteliveresultsclient.LoginHandler.java

public List<NameValuePair> getFormParams(String html, String username, String password)
        throws UnsupportedEncodingException {

    Document doc = Jsoup.parse(html);

    // Google form id
    Element loginform = doc.getElementById("primarycontent");
    Elements inputElements = loginform.getElementsByTag("input");

    List<NameValuePair> paramList = new ArrayList<NameValuePair>();

    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("email")) {
            value = username;//  www .  j av  a2s.  co m
        } else if (key.equals("password")) {
            value = password;
        }

        paramList.add(new BasicNameValuePair(key, value));

    }

    return paramList;
}