mardi 3 juillet 2018

Returning a json after extracting text from web pages

I'm trying to build a simple web scraper that extracts some text from a web page, puts the text in a json and than my main goal is to show the json on a web page. Right now i'm extracting the text but i'm not really sure what would be the best way to do this if i want it to be shown on a web page. Basically this is the code:

public void searchOnline() {
        try {
            final Document page = Jsoup.connect("link" + URLEncoder.encode(query, "UTF-8")).userAgent(userAgent).get();
            final List<SearchResult> resultList = new ArrayList<SearchResult>();

            for(Element searchResult : page.select(".offer-title__link")) {
                final String title = searchResult.text();
                final String url = searchResult.attr("href");
                resultList.add(new SearchResult(title, url));
            }

          OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(new File("results.json"), resultList);

        }catch(Exception e) {
            e.printStackTrace();
        }

    }

That object_mapper just creates a json file and returns void and i want this method to return that json somehow and than i need to show it on the page. Do you guys have any clue on how should i do that?Or are there any other ways to put all of the extracted text into a json and than return that json?




Aucun commentaire:

Enregistrer un commentaire