Narzędzia do przechwytywania i konwertowania Internetu

Przechwytuj tabele HTML ze stron internetowych za pomocą PHP

API PHP

Istnieje wiele sposobów konwersji tabel HTML into Używanie arkuszy kalkulacyjnych JSON, CSV lub Excel PHP API GrabzIt, tutaj wyszczególniono niektóre z najbardziej przydatnych technik. Jednak zanim zaczniesz, pamiętaj, że po wywołaniu URLToTable, HTMLToTable or FileToTable metody Save or SaveTo należy wywołać metodę, aby przechwycić tabelę. Jeśli chcesz szybko sprawdzić, czy ta usługa jest właśnie dla Ciebie, możesz spróbować pokaz na żywo przechwytywania tabel HTML z adresu URL.

Podstawowe opcje

Poniższy przykład kodu automatycznie konwertuje pierwszą tabelę HTML odkrytą na określonej stronie internetowej into dokument CSV.

$grabzIt->URLToTable("https://www.tesla.com");
//Then call the Save or SaveTo method
$grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>");
//Then call the Save or SaveTo method
$grabzIt->FileToTable("tables.html");
//Then call the Save or SaveTo method

Domyślnie spowoduje to konwersję pierwszej zidentyfikowanej tabeli into stole. Jednak drugą tabelę na stronie internetowej można przekonwertować, przekazując 2 do setTableNumberToInclude Metoda.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setTableNumberToInclude(2);

$grabzIt->URLToTable("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setTableNumberToInclude(2);

$grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setTableNumberToInclude(2);

$grabzIt->FileToTable("tables.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.csv");

Można również użyć setTargetElement Metoda zapewniająca konwersję tylko tabel w ramach określonego identyfikatora elementu.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setTargetElement("stocks_table");

$grabzIt->URLToTable("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setTargetElement("stocks_table");

$grabzIt->HTMLToTable("<html><body><table id='stocks_table'><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.csv");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setTargetElement("stocks_table");

$grabzIt->FileToTable("tables.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.csv");

Alternatywnie możesz przechwycić wszystkie tabele na stronie internetowej, przekazując true do setIncludeAllTables Metoda ta będzie jednak działać tylko z formatami XLSX i JSON. Ta opcja umieści każdą tabelę w nowym arkuszu w wygenerowanym skoroszycie arkusza kalkulacyjnego.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setFormat('xlsx');
$options->setIncludeAllTables(true);

$grabzIt->URLToTable("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.xlsx");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setFormat('xlsx');
$options->setIncludeAllTables(true);

$grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.xlsx");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setFormat('xlsx');
$options->setIncludeAllTables(true);

$grabzIt->FileToTable("tables.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.xlsx");

Konwertuj tabele HTML na JSON

Czasami istnieje potrzeba programowego odczytywania tabel HTML, GrabzIt pozwala to zrobić za pomocą PHP poprzez konwersję internetowych tabel HTML into JSON. Aby to zrobić, określ json jako parametr formatu. Na przykład w poniższym przykładzie konwertujemy tabelę HTML synchronicznie następnie za pomocą wbudowanego json_decode Metoda PHP do parsowania JSON string into obiekt, z którym możemy łatwo pracować.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setFormat("json");
$options->setTableNumberToInclude(1);

$grabzIt->URLToTable("https://www.tesla.com", $options);

$json = $grabzIt->SaveTo();
if ($json != null)
{
    $tableObj = json_decode($json);
}
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setFormat("json");
$options->setTableNumberToInclude(1);

$grabzIt->HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", $options);

$json = $grabzIt->SaveTo();
if ($json != null)
{
    $tableObj = json_decode($json);
}
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
    
$options = new \GrabzIt\GrabzItTableOptions();
$options->setFormat("json");
$options->setTableNumberToInclude(1);

$grabzIt->FileToTable("tables.html", $options);

$json = $grabzIt->SaveTo();
if ($json != null)
{
    $tableObj = json_decode($json);
}

Niestandardowy identyfikator

Możesz przekazać niestandardowy identyfikator do stół metody pokazane poniżej, ta wartość jest następnie zwracana do twojego programu obsługi GrabzIt PHP. Na przykład ten niestandardowy identyfikator może być identyfikatorem bazy danych, umożliwiając powiązanie wyodrębnionej tabeli z określonym rekordem bazy danych.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setCustomId(123456);

$grabzIt->URLToTable("https://www.tesla.com", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setCustomId(123456);

$grabzIt->HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItTableOptions();
$options->setCustomId(123456);

$grabzIt->FileToTable("example.html", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");