Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got a task at school in which I have to do the following:
Implement the RESTful endpoint API, which simultaneously makes calls to the following websites:

https://pizzerijalimbo.si/meni/
https://pizzerijalimbo.si/kontakt/
https://pizzerijalimbo.si/my-account/
https://pizzerijalimbo.si/o-nas/

The input for the endpoint is ‘integer’, which represents the number of simultaneous calls to the above web pages (min 1 represents all consecutive calls, max 4 represents all simultaneous calls).

Extracts a short title text from each page and saves this text in a common global structure (array, folder (). The program should also count successful calls. Finally, the service should list the number of successful calls, the number of failed calls and the saved address texts from all web pages.


With some help I managed to do something, but I still need help with data exstraction using Jsoup or any other method.

Here is the code that I have:


import java.util.Arrays;
import java.util.List;

import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

@RestController
public class APIcontroller {
    
    @Autowired
    private RestTemplate restTemplate;

    List<String> websites = Arrays.asList("https://pizzerijalimbo.si/meni/", 
        "https://pizzerijalimbo.si/kontakt/", 
        "https://pizzerijalimbo.si/my-account/", 
        "https://pizzerijalimbo.si/o-nas/");

    @GetMapping("/podatki")
    public List<Object> getData(@RequestParam(required = true) int numberOfWebsites) {
        List<String> websitesToScrape = websites.subList(0, numberOfWebsites);
    
        for (String website : websitesToScrape) {
            Document doc = Jsoup.connect("https://pizzerijalimbo.si/meni/").get();
            log(doc.title());
            Elements newsHeadlines = doc.select("#mp-itn b a");
            for (Element headline : newsHeadlines) {
              log("%s\n\t%s", 
                headline.attr("title"), headline.absUrl("href"));
            }
        }
    }
}


I also need to do it parallel, so the calls to a secific website go on at the same time.
But the main problem now is with the log funcion which does not work properly.


What I have tried:

I tried to solve the problem using Jsoup library, but I dont seem to undersand it well, so I got an error in the for loop which says that the method log is undefined.
Posted
Comments
Richard MacCutchan 12-Sep-21 7:01am    
"I got an error in the for loop which says that the method log is undefined."
If that is not a method that you are supposed to write, which class is it a member of?
AquaBalls 12-Sep-21 7:23am    
I actually copied that line of code from https://jsoup.org/ . I don't really understand it well so I need help with that.
Richard MacCutchan 12-Sep-21 7:38am    
Well that is a good lesson for you: do not copy code from the internet if you don't understand it.
AquaBalls 12-Sep-21 7:36am    
To answer you question. It is a member of a public class APIcontroller.
Richard MacCutchan 12-Sep-21 7:39am    
I do not see it in your code above, so where have you defined it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900