웹 캡처 및 변환 도구

Java를 사용하여 웹 사이트에서 HTML 테이블 캡처

자바 API

HTML 테이블을 변환하는 방법에는 여러 가지가 있습니다 into JSON, CSV 및 엑셀 스프레드 시트 GrabzIt은 Java API입니다여기에 가장 유용한 기술이 자세히 설명되어 있습니다. 그러나 시작하기 전에 전화를 한 후 URLToTable, HTMLToTable or FileToTable 방법 Save or SaveTo 테이블을 캡처하려면 메소드를 호출해야합니다. 이 서비스가 귀하에게 적합한 지 신속하게 확인하려면 HTML 테이블 캡처 라이브 데모 URL에서.

기본 옵션

이 코드 스 니펫은 지정된 웹 페이지에서 찾은 첫 번째 HTML 테이블을 변환합니다. intoa 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

기본적으로 이것이 식별하는 첫 번째 테이블을 변환합니다 intoa 테이블. 그러나 웹 페이지의 두 번째 테이블은 2를 setTableNumberToInclude 방법 TableOptions 클래스입니다.

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

TableOptions options = new TableOptions();
options.setTableNumberToInclude(2);

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

TableOptions options = new TableOptions();
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");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTableNumberToInclude(2);

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

당신은 또한 사용할 수 있습니다 setTargetElement 지정된 요소 ID 내의 테이블 만 변환되도록하는 메소드입니다.

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

TableOptions options = new TableOptions();
options.setTargetElement("stocks_table");

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

TableOptions options = new TableOptions();
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");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTargetElement("stocks_table");

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

또는 웹 페이지에 true를 전달하여 웹 페이지의 모든 테이블을 캡처 할 수 있습니다. setIncludeAllTables 그러나이 방법은 XLSX 및 JSON 형식에서만 작동합니다. 이 옵션은 각 테이블을 생성 된 스프레드 시트 통합 문서 내의 새 시트에 넣습니다.

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

TableOptions options = new TableOptions();
options.setFormat(TableFormat.XLSX);
options.setIncludeAllTables(true);

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

TableOptions options = new TableOptions();
options.setFormat(TableFormat.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");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setFormat(TableFormat.XLSX);
options.setIncludeAllTables(true);

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

HTML 테이블을 JSON으로 변환

GrabzIt은 웹에서 찾은 HTML 테이블을 JSON으로 변환 할 수도 있습니다. 대신 JSON 형식 만 지정하면됩니다. 아래 예에서 데이터는 동 기적으로 읽히고 GrabzItFile 를 사용하여 개체 SaveTo 방법, 그러나 일반적으로이 작업을 수행하는 것이 좋습니다 비동기 적으로.

변환이 완료되면 toString 메소드를 JSON으로 가져 오기 위해 호출됩니다. string다음과 같은 라이브러리로 파싱 할 수 있습니다. 구글 gson.

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

TableOptions options = new TableOptions();
options.setFormat(TableFormat.JSON);
options.setTableNumberToInclude(1);

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

GrabzItFile file = grabzIt.SaveTo();
if (file != null)
{
    String json = file.toString();
}

맞춤식 식별자

사용자 지정 식별자를 테이블 아래 표시된대로 메소드를 사용하면이 값이 GrabzIt Java 핸들러로 리턴됩니다. 예를 들어이 사용자 지정 식별자는 데이터베이스 식별자 일 수 있으며 스크린 샷을 특정 데이터베이스 레코드와 연결할 수 있습니다.

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

TableOptions options = new TableOptions();
options.setCustomId("123456");

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

TableOptions options = new TableOptions();
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");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setCustomId("123456");

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