Comment puis-je extraire des données d'un site Web à l'aide de Java (Android)?


Mon application Android obtiendra les informations du transporteur à partir d'un numéro de téléphone. J'ai l'intention d'utiliser Jsoup (ou un autre analyseur HTML Java) pour gratter les informations de transporteur affichées dans un tableau.

J'essaie de gratter de fonefinder.net

Le format de l'URL de la requête est:

http://www.fonefinder.net/findome.php?npa=**first 3 digits**&nxx=**next 3 digits**&thoublock=**final 4 digits**

Le HTML de la page est un tableau simple (voir ci-dessous). J'essaie d'extraire des données de la 2ème ligne, 5ème colonne, où un lien apparaît dans le format

http://fonefinder.net/(CARRIER_NAME).php

Où CARRIER_NAME est une valeur comme "Verizon". J'ai besoin d'aide pour comprendre comment extraire ces données.

<table border="3" cellspacing="2" cellpadding="2" bgcolor="#FFFFCC">
  <tbody>
    <tr bgcolor="#7093DB" align="CENTER">
      <th>
        Area Code
      </th>
      <th>Prefix</th>
      <th>
        City/Switch Name
        <br>
        (Click for city search)
      </th>
      <th>
        State/Prov.
        <br>
        Area Map
      </th>
      <th>
        Telephone Company
        <br/>
        Web link
      </th>
      <th>
        Telco
        <br/>
        Type
      </th>
      <th>
        Map/Zip
        <br/>
        Detail
      </th>
    </tr>
    <tr>
      <td>
        **first 3 digits**
      </td>
      <td>
        **next 3 digits**
      </td>
      <td>
        City Name
      </td>
      <td>
        State Name
      </td>
      <td>
        <a href="http://fonefinder.net/CARRIER_NAME.PHP">carrier name</a>
      </td>
      <td>WIRELESS PROV</td>
      <td>
        map
      </td>
    </tr>
  </tbody>
</table>
Author: Midhun MP, 2013-01-03

1 answers

Le code que j'ai écrit utilise beaucoup la syntaxe du sélecteur de Jsoup pour analyser les balises par nom, mais vous pouvez également analyser par classe CSS, id, attribut et plus encore. La documentation de syntaxe Jsoup selector contient la liste complète des sélecteurs que vous pouvez utiliser.

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class StackOverflowQuestion {

    public static void main(String[] args) {

        try {
            // get the tables on this page, note I masked the phone number
            Document doc = Jsoup.connect("http://www.fonefinder.net/findome.php?npa=###&nxx=###&thoublock=####").get();
            Elements tables = doc.select("table");

            // get the second table, 0 indexed
            Element secondTable = tables.get(1);

            // get the columns
            Elements tds = secondTable.select("td");

            //get the 5th column, 0 indexed
            Element fifthTd = tds.get(4);

            //get the link in this column
            Element link = fifthTd.select("a").first();

            //print out the URL
            System.out.println(link.attr("href"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 0
Author: Richard Krajunus, 2013-02-21 03:13:51