Java - Retrieving data from a website
-
29-05-2021 - |
Domanda
I'm making an application that recovers lottery numbers and displays them in a window. However I've unsure of how I would recover the data and the numbers from the website:
https://www.national-lottery.co.uk/player/p/results.ftl
How would you go about doing this? I have before done this but with a site that returned a String of data which I could use. I'm more unsure of how to do this. Any suggestions would be appreciated and the technique (if there is one) will help me in a lot more of my projects!
Soluzione
The site provides a link to download a CSV version of the numbers. Just use that:
https://www.national-lottery.co.uk/player/lotto/results/downloadResultsCSV.ftl
It looks like:
DrawDate,Ball 1,Ball 2,Ball 3,Ball 4,Ball 5,Ball 6,Bonus Ball,Ball Set,Machine 07-Apr-2012,23,12,42,16,25,31,18,6,LANCELOT 04-Apr-2012,44,23,9,40,33,26,31,2,MERLIN 31-Mar-2012,2,49,40,47,18,5,19,1,MERLIN 28-Mar-2012,16,8,39,22,3,38,26,3,MERLIN 24-Mar-2012,24,27,6,39,31,45,32,4,LANCELOT 21-Mar-2012,10,14,45,25,39,21,40,1,MERLIN 17-Mar-2012,37,40,1,3,20,16,15,2,MERLIN 14-Mar-2012,15,36,26,31,14,18,48,4,MERLIN 10-Mar-2012,12,37,23,43,3,1,33,1,MERLIN 07-Mar-2012,28,44,8,35,11,2,17,3,MERLIN 03-Mar-2012,31,20,40,28,7,23,42,4,MERLIN 29-Feb-2012,41,29,46,14,49,13,43,3,LANCELOT 25-Feb-2012,29,27,26,7,32,25,33,1,LANCELOT 22-Feb-2012,35,12,7,49,43,15,8,4,MERLIN 18-Feb-2012,19,22,30,33,41,2,24,4,LANCELOT 15-Feb-2012,30,40,28,33,9,44,16,3,MERLIN 11-Feb-2012,24,31,23,1,49,45,6,3,LANCELOT 08-Feb-2012,7,13,31,44,36,16,26,8,LANCELOT 04-Feb-2012,41,45,7,40,48,4,46,2,MERLIN 01-Feb-2012,7,39,38,17,22,21,3,2,LANCELOT 28-Jan-2012,10,25,31,40,28,12,1,2,LANCELOT 25-Jan-2012,2,30,8,26,45,39,46,1,MERLIN 21-Jan-2012,17,5,32,39,49,42,19,5,MERLIN 18-Jan-2012,22,43,34,9,31,35,20,6,MERLIN 14-Jan-2012,7,12,10,15,25,42,33,7,LANCELOT 11-Jan-2012,40,33,39,9,2,27,45,6,LANCELOT 07-Jan-2012,47,8,15,17,14,20,38,7,MERLIN 04-Jan-2012,42,43,30,9,28,26,2,8,MERLIN 31-Dec-2011,11,38,42,37,44,7,2,7,LANCELOT 28-Dec-2011,48,11,49,13,17,8,19,6,LANCELOT 24-Dec-2011,43,32,36,15,23,1,19,7,LANCELOT 21-Dec-2011,30,7,28,34,38,45,6,5,MERLIN 17-Dec-2011,42,1,35,48,39,22,12,5,MERLIN 14-Dec-2011,3,43,30,28,10,25,31,8,MERLIN 10-Dec-2011,30,21,29,39,24,16,20,6,LANCELOT 07-Dec-2011,10,31,27,47,32,14,41,5,MERLIN 03-Dec-2011,49,1,35,48,47,30,8,8,MERLIN 30-Nov-2011,30,26,25,24,23,13,4,7,MERLIN 26-Nov-2011,13,36,26,16,25,46,15,6,MERLIN 23-Nov-2011,19,31,48,22,4,11,6,5,MERLIN 19-Nov-2011,32,31,1,34,29,36,45,3,ARTHUR 16-Nov-2011,26,40,39,27,10,12,20,1,GUINEVERE 12-Nov-2011,28,13,12,33,6,38,10,14,ARTHUR 09-Nov-2011,27,2,8,32,23,10,44,1,GUINEVERE 05-Nov-2011,14,24,39,23,16,27,43,8,LANCELOT 02-Nov-2011,12,38,11,33,37,49,3,2,GUINEVERE 29-Oct-2011,49,14,5,28,9,46,45,1,GUINEVERE 26-Oct-2011,4,23,34,41,38,39,27,4,GUINEVERE 22-Oct-2011,20,43,27,44,28,34,1,4,ARTHUR 19-Oct-2011,13,18,34,49,32,14,20,3,GUINEVERE 15-Oct-2011,41,7,12,46,34,27,14,2,ARTHUR 12-Oct-2011,37,26,40,25,13,24,30,3,ARTHUR
Altri suggerimenti
Use Jsoup to retrieve and parse page:
String url = "https://www.national-lottery.co.uk/player/p/results.ftl";
Document document = Jsoup.connect(url).get();
final Elements elementsByTag = document.getElementsByTag("table");
... then work with the table or any other element
Create a URL representation of the page address. Open a connection to the URL. Establish an input stream. Read all of the data from the stream. This will be the page source.
URL url = new URL("https://www.national-lottery.co.uk/player/p/results.ftl");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
byte[] data = new byte[stream.available()];
stream.read(data);
stream.close();
String source = new String(data);
Unless the site provides an api or web service that allows querying for lottery numbers, you may have to scrape the html source of the page. It looks like the numbers are stored in a simple html list:
<ul>
<li>12</li>
<li>16</li>
<li>23</li>
<li>25</li>
<li>31</li>
<li>42</li>
<li class="bonus">18</li>
</ul>
There are lots of good Java HTML parsers out there. Here's a few projects:
- http://jsoup.org/
- http://htmlparser.sourceforge.net/
- http://mozillaparser.sourceforge.net/
- http://jtidy.sourceforge.net/
I looked around the site you're interested in and it appears they have a "history" page with the lottery numbers over several days:
https://www.national-lottery.co.uk/player/lotto/results/results.ftl
That's probably a better page to scrape from.