Come faccio a raschiare i dati da un sito web utilizzando Java (Android)?


La mia applicazione Android otterrà informazioni carrier da un numero di telefono. Intendo utilizzare Jsoup (o un altro parser HTML Java) per raschiare le informazioni del vettore visualizzate in una tabella.

Sto tentando di raschiare da fonefinder.net

Il formato dell'URL della query è:

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

L'HTML della pagina è una semplice tabella (vedi sotto). Sto cercando di estrarre i dati dalla 2a riga, 5a colonna, dove appare un collegamento nel formato

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

Dove CARRIER_NAME è un valore come "Verizon". Ho bisogno di aiuto per capire come estrarre questi dati.

<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

Il codice che ho scritto fa un uso pesante della sintassi del selettore di Jsoup per analizzare i tag per nome, ma puoi anche analizzare per classe CSS, id, attributo e altro ancora. La Jsoup selector syntax documentation contiene l'elenco completo dei selettori che è possibile utilizzare.

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