我正在从页面https://br.investing.com/indices/major-indices上的表中提取数据
我可以完美地提取数据,但是我不知道如何以以下格式写入Json。
用于数据提取的代码:
public void AcessarPagina() {
System.out.println("*** Abrindo a Pagina de Extração *** \n");
driver.get("https://br.investing.com/indices/major-indices");
WebDriverWait AguardarTabela = new WebDriverWait(driver, 20);
AguardarTabela.until(ExpectedConditions.presenceOfElementLocated(By
.id("cross_rates_container")));
System.out.println("*** Iniciando a Extração *** \n");
WebElement TabelaCabecalho = driver.findElement(By.id("cross_rates_container"));
List<WebElement> Cabecalho = TabelaCabecalho.findElements(By.tagName("th"));
for (int i = 0; i < Cabecalho.size(); i++) {
System.out.print(Cabecalho.get(i).getText() + ";");
}
WebElement TabelaDados = driver.findElement(By.id("cross_rates_container"));
List<WebElement> Linhas = TabelaDados.findElements(By.tagName("tr"));
List<WebElement> ListaColunas = null;
for (WebElement row : Linhas) {
System.out.println();
ListaColunas = row.findElements(By.tagName("td"));
for (WebElement column : ListaColunas) {
System.out.print(column.getText() + ";");
}
}
}
我需要的Json格式:
[ {
"indice" : "Dow 30",
"ultimo" : "23.185,62",
"maxima" : "23.189,76",
"minima" : "21.285,37",
"variacao" : "+1.985,00",
"variacao2" : "+9,36%",
"hora" : "13/03"
}, {
"indice" : "S&P 500 VIX",
"ultimo" : "23.185,62",
"maxima" : "23.189,76",
"minima" : "21.285,37",
"variacao" : "+1.985,00",
"variacao2" : "+9,36%",
"hora" : "13/03"
} ]
问题来源:Stack Overflow
将对象映射到Java对象,然后使用Chris提到的Jackson可能是最好的选择。我整理了一个示例,说明您可以做什么:
这是可以保存您的值的Java对象:
public class TableElement {
private String indice;
private String ultimo;
private String maxima;
private String minima;
private String variacao;
private String variacao2;
private String hora;
public String getIndice() {
return indice;
}
public void setIndice(String indice) {
this.indice = indice;
}
// Add all getters and setters ...
}
注意-需要使用getter和setter方法,因为这是杰克逊用来从/向对象检索/设置对象值的方法 json
并将其实际映射到json这样的地方:
StockTable stockTable = new StockTable();
List<TableElement> stocks = new ArrayList<>();
TableElement tableElement = new TableElement();
tableElement.setIndice("Dow 30");
tableElement.setUltimo("23.185,62");
tableElement.setMaxima("23.189,76");
tableElement.setMinima("21.285,37");
tableElement.setVariacao("+1.985,00");
tableElement.setVariacao2("+9,36%");
tableElement.setHora("13/03");
TableElement tableElement2 = new TableElement();
tableElement2.setIndice("S&P 500 VIX");
tableElement2.setUltimo("23.185,62");
tableElement2.setMaxima("23.189,76");
tableElement2.setMinima("21.285,37");
tableElement2.setVariacao("+1.985,00");
tableElement2.setVariacao2("+9,36%");
tableElement2.setHora("13/03");
stocks.add(tableElement);
stocks.add(tableElement2);
ObjectMapper objectMapper = new ObjectMapper();
try {
System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(stocks));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
它将产生如下所示的输出:
[ {
"indice" : "Dow 30",
"ultimo" : "23.185,62",
"maxima" : "23.189,76",
"minima" : "21.285,37",
"variacao" : "+1.985,00",
"variacao2" : "+9,36%",
"hora" : "13/03"
}, {
"indice" : "S&P 500 VIX",
"ultimo" : "23.185,62",
"maxima" : "23.189,76",
"minima" : "21.285,37",
"variacao" : "+1.985,00",
"variacao2" : "+9,36%",
"hora" : "13/03"
} ]
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。