Reading Data (Numbers) from a Website - java

i would like to create an excel file with data from a website. In my inputstream i find something from the page but not the things i am looking for.
This is the website i want the data from: https://www.finanzen.net/bilanz_guv/adidas
As an Example i would like a System.out.println that returns the earning per share ( in german: Ergebnis je Aktie" ) from the years 2011 to 2017 so it would be the following numbers:
3,20 2,51 3,76 2,35 3,30 5,08 6,69
What i have managed till now:
URL u = new URL("https://www.finanzen.net/bilanz_guv/adidas");
InputStream in = u.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
But the result String does not contain any of the searched numbers.
It does contain the first line of the pages soucecode so as if i clicked on show the soucecode of the webpage in my browser.
As i have not much knowledge about programming keep the answeres simple :-)
Thanks

Related

how can i get spesific words from an url in java

How can i get spesific words from an url in java. Like i want to take datas from class which calling like blablabla.
Here is my code.
URL url = new URL("https://www.doviz.com/");
URLConnection connect = url.openConnection();
InputStream is = connect.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = br.readLine()) != null)
{
System.out.println(line);
}
Take a look at Jsoup , this will allow you to get the content of a web page and NOT the HTML code. Let's say it will play the role of the browser, it will parse the HTML tags into a human readable text.
Once you will get the content of your page in a String, you can count the occurrences of your word using any algorithm of occurrences count.
Simple example to use it:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/* ........ */
String URL = "https://www.doviz.com/";
Document doc = Jsoup.connect(URL).get();
String text = doc.body().text();
System.out.println(text);
EDIT
If you don't want to use a parser (as you mentioned in the comment that you don't want external libraries), you will get the whole HTML code of the page, that's how you can do it
try {
URL url = new URL("https://www.doviz.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
str = in.readLine().toString();
System.out.println(str);
/*str will get each time the new line, if you want to store the whole text in str
you can use concatenation (str+ = in.readLine().toString())*/
}
in.close();
} catch (Exception e) {}

Convert XML to JSON efficiently for Huge Files

I have several XML files ( in size of GBs ) that are to be converted to JSON. I am easily able to convert small sized files ( in KiloBytes ) using the JSON library ( org.json - https://mvnrepository.com/artifact/org.json/json/20180813 ).
Here's the code that i am using
static String line="",str="";
BufferedReader br = new BufferedReader(new FileReader(link));
FileWriter fw = new FileWriter(outputlink);
JSONObject jsondata = null;
while ((line = br.readLine()) != null)
{
str+=line;
}
jsondata = XML.toJSONObject(str);
But the large files ( even the <100 MB ones ) are taking too long to process and the larger ones are throwing java.lang.OutOfMemoryError: Java heap space. So, how to optimize the code to process large files ( or any other approach/library ).
UPDATE
I have updated the code and I am writing XML into JSON segment by segment
My XML :
<PubmedArticleSet>
<PubmedArticle>
</PubmedArticle>
<PubmedArticle>
</PubmedArticle>
...
</PubmedArticleSet>
So I am ignoring the root node <PubmedArticleSet> ( I will add it later ) converting each <PubmedArticle> </PubmedArticle> to JSON and writing at a time
br = new BufferedReader(new FileReader(link));
fw = new FileWriter(outputlink,true);
StringBuilder str = new StringBuilder();
br.readLine(); // to skip the first three lines and the root
br.readLine();
br.readLine();
while ((line = br.readLine()) != null) {
JSONObject jsondata = null;
str.append(line);
System.out.println(str);
if (line.trim().equals("</PubmedArticle>")) { // split here
jsondata = XML.toJSONObject(str.toString());
String jsonPrettyPrintString = jsondata.toString(PRETTY_PRINT_INDENT_FACTOR);
fw.append(jsonPrettyPrintString.toString());
System.out.println("One done"); // One section done
str= new StringBuilder();
}
}
fw.close();
I am no longer getting the HeapError but still the processing is taking hours for ~300 MB range files. Kindly provide any suggestions to speed up this process.
This statement is the main reason that kills your performance:
str+=line;
This causes the allocation, copying and deallocation of numerous of String objects.
You need to use a StringBuilder:
StringBuilder builder = new StringBuilder();
while ( ... ) {
builder.append(line);
}
It may also help (to a lesser extent) to read the file in larger chunks and not line by line.
The IO operation of reading a large file is very time consuming. Try utilizing a library to handle this for you. For example with apache commons IO:
File xmlFile= new File("D:\\path\\file.xml");
String xmlStr= FileUtils.readFileToString(xmlFile, "UTF-8");
JSONObject xmlJson = XML.toJSONObject(xmlStr);

Android BufferedReader does not read the whole response

I have a problem with reading a ULR response. On Android it only reads around the half of the response.
If I use the same code in a normal Java project everything works fine.
try {
String _output = null;
URL url = new URL("http://example.com");
BufferedReader buffer = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder everything = new StringBuilder();
String line;
while ((line = buffer.readLine()) != null) {
everything.append(line);
}
_output = everything.toString();
buffer.close();
System.out.print(_output);
} catch (IOException e) {
e.printStackTrace();
}
How do you know that it's only half of the response? If you rely on what is printed with System.out.println() then you should be aware that Logcat has a limitation that prevents it from printing more than 4,000 characters. Anything after that is truncated. To check how much of the response you have, you could print everything.length()first, ot see if you are in that situation.
You can look at this existing question on SO for reference, but there are many others.

Android Java Get simple .php page content into a String and save to internal storage

I'm developing an app. That app needs to get the content of a simple .php URL, and save it as a String.
The problem is that it is a very long String (VERY LONG) and it get's but in half. Take this link as an example:
http://thuum.org/download-dev-notes-web.php
With this code
URL notes = new URL("http://thuum.org/download-dev-notes-web.php")
BufferedReader in = new BufferedReader(new InputStreamReader(notes.openStream()));
String t = "";
while ((inputLine = in.readLine()) != null)
t = inputLine;
fOut = openFileOutput("notes", MODE_PRIVATE);
fOut.write(t.getBytes());
// Added This \/ to see it's length when divided, and it is not nearly as much as it should be
System.out.println(t.split("\\#").length);
Can someone tell me how would I be able to download that into a String, and save it into the internal storage without it getting cut? Some why it looks like it gets only the last x digits...
it seems you're overwriting your String t in every iteration of the while-loop. Try this:
StringBuilder result = new StringBuilder();
String inputLine = "";
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
fOut = openFileOutput("notes", MODE_PRIVATE);
fOut.write(result.toString().getBytes());
It creates a mutable StringBuilder and uses the resulting (immutable) String in the write call.
edit: I also recommend to always use curly brackets to indicate end of loop bodies, ommiting those can quickly lead to bugs, just check #gotofail for a recent example ;-)

How to store data retrieve from php file with java

my question is how do we store data from php file at web using java
(i can view the php file but cant store it into my array variable). may this benefit other.
//http://sampleonly.com.my/getInfo.php //this url is not exist. just for example
<?php
echo("Ridzuan");
echo("split");
echo("Malaysia");
echo("split");
?>
// i want to get the echo "Ridzuan" and "Malaysia". i dont want echo "split".
below is my current code
URL connectURL = new URL("http://sampleonly.com.my/getInfo.php");
BufferedReader in = new BufferedReader(
new InputStreamReader(connectURL.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
//array below should store input from .php file after i thrown "split" text
String[] strArray2 = inputLine.split(Pattern.quote("split"));
in.close();
error output:
Exception in thread "main" java.lang.NullPointerException
i have refer to this question, Retrieving info from a file but confuse to understand the code. perhap any good people here can provide me with valid code on how to store echo data from php file to my java array variable.
thanks in advance folk.
ANSWER credit to JJPA
URL connectURL = new URL("http://vmalloc.in/so.php");
BufferedReader in = new BufferedReader(
new InputStreamReader(connectURL.openStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
sb.append(inputLine);
}
String[] strArray2 = sb.toString().split(Pattern.quote("split"));
System.out.println(strArray2[0]);
System.out.println(strArray2[1]);
in.close();
output result:
Ridzuan
Malaysia
just like what i wanted
Yes you should get that exception in inputLine. To know I recommend you to debug your code.
As a solution try the below code.
URL connectURL = new URL("http://vmalloc.in/so.php");
BufferedReader in = new BufferedReader(new InputStreamReader(
connectURL.openStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
sb.append(inputLine);
}
// array below should store input from .php file after i thrown "split"
// text
String[] strArray2 = sb.toString().split("split");
System.out.println(strArray2);
in.close();
Use flower bases for while block. Otherwise you are using a null inputLine after the while block. That is because you are leaving the loop when inputLine is null. And hence, when tried to use the same, it threw a NullPointerException.
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
//array below should store input from .php file after i thrown "split" text
String[] strArray2 = inputLine.split(Pattern.quote("split"));
// do whatever you want with this array
} // while
You are getting NullPointerException because your inputLine is NULL. You are running the loop until the inputLine is NULL and then after the loop is terminated, you are using that NULL variable to get the php result. Instead, store it in a temporary variable, either String or array according to your need.
For example, if you need to store it in a string, you can do it as follows
String inputLine, temp="";
while ((inputLine = in.readLine()) != null){
temp.concat(inputLine);
System.out.println(inputLine);
}
And then use the variable temp to access the result.

Categories