简体   繁体   English

将页面中提取的数据写入 Json 文件

[英]Write Json file the data extracted from a page

I am extracting the data from the table on the page https://br.investing.com/indices/major-indices我正在从页面上的表格中提取数据https://br.investing.com/indices/major-indices

I can extract the data perfectly, but I don't know how to write to a Json, in the format below.我可以完美地提取数据,但我不知道如何以下面的格式写入Json。

Selenium code for data extraction:用于数据提取的硒代码:

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 format I need:我需要的 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"
} ]

Mapping the object to a Java object, then using Jackson as Chris mentioned is probably the best bet.将对象映射到 Java 对象,然后使用 Chris 提到的 Jackson 可能是最好的选择。 I have put together an example of what you can do:我整理了一个你可以做什么的例子:

Here is the Java object that could hold your values:这是可以保存您的值的 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 ...
}

Note - getters and setters are required because that's what jackson uses to retrieve/set values on an object from and to json注意 - getter 和 setter 是必需的,因为这是 jackson 用来从json检索/设置对象上的值的方法

And actually mapping it to json would go something like this:实际上将它映射到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();
}

And it would produce output that looks like this:它会产生如下所示的输出:

[ {
  "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"
} ]

最简单的可能是定义一个具有这些属性(索引等)的类,并在解析数据行时创建添加到列表的实例,然后只需使用 Gson 或 Jackson 将对象列表转换为 JSON,例如如何创建 JSONArray对于 List<Class name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将提取的图像从pdf写入文件 - How to write extracted image from pdf to a file 如何从Zip文件中提取Json数据并将提取的Json文件发送到前端? - How to extract Json data from Zip file and to send the extracted Json file to front-end? 如何加载JSP页面并同时在json文件上写入数据? - How to load a JSP page and write data on a json file simultaneously? 从文本文件提取的数据将其存储在变量中时出错 - data extracted from text file gives error when storing it in variables 使用从XML文件中提取的数据创建正则表达式 - Create a regular expression using data extracted from an XML file 将数据写入 JSON 文件,将 JSON 文件从客户端发送到服务器,读取 JSON - Write data in JSON file, send JSON file from client to server, read JSON Java servlet,将数据从文本文件写入网页 - Java servlets, write data from text file to web page 从网站Api提取的Json Code解析数据时输出黑屏 - Blank screen output when Parsing data from Json Code extracted from a website Api 如何将使用wsadmin从websphere提取的ConfigProperties_server1.props转换为xml或json文件? - How to convert the ConfigProperties_server1.props which is extracted from websphere using wsadmin to xml or json file? 以JAVA格式在JSON文件中写入数据 - Write data in JSON file in right form JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM