Hello i want to understand how to get a value specific all the prices from here example http://www.ebay.com/sch/i.html?_sacat=0&_nkw=iphone+5&_frs=1
and return the values so i can lets say add them into database along with the product name.
String weburl = "http://www.ebay.com/sch/i.html?_sacat=0&_nkw=iphone+5&_frs=1";
URL oracle = new URL(weburl);
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String line;
while ((line = in.readLine()) != null)
{
if (line.contains("EUR</b>"))
{
String command = line.split("EUR</b>").toString();
final String value = command.substring(8);
final StringTokenizer s = new StringTokenizer(value, " ");
final String DurationString = s.nextToken();
System.out.println("Timh: " + DurationString);
}
}
in.close();
this does not work for me until now.
How should i change it ?
You can use jSoup for this.
Note that eBay offers various APIs that will suit for your purposes.
https://go.developer.ebay.com/
Related
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) {}
Basically I'm trying to get a string from an API,and this API is just a blank page with one line which has all the information needed, and I'm only trying to get a part of that.
This part is an ID which for every person - has the same amount of characters.
The API has for each person this line:
{"id":"anExampleUniqueWhichHas32Charact","name": "Player"}
I kinda changed the code so you'll understand, because I'm using a library dedicated for that, but I'm just trying to get the web scraping right.
So what I tried to do was Web Scrape and get the string.length of that amount.
But it doesn't work.
I know I can also use Regex for patterns, but I don't really know how to use that. Regex would honestly be more helpful in this situation.
public void checkAPI() throws IOException {
String person = userInput.nextLine(); // It's just any name.
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" +
person);
URLConnection con = url.openConnection();
InputStream isr =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(isr));
String line;
while ((line = br.readLine()) != null) {
if (line.length() == 32) {
System.out.println(line);
}
}
}
I currently just expect the line to print, later when it'll work I'll use it for other stuff.
No errors are being thrown.
The API uses Json. https://de.wikipedia.org/wiki/JavaScript_Object_Notation
You can use a standard json parser like jackson https://en.wikipedia.org/wiki/Jackson_(API) to parse and query the result.
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(new URL("https://api.mojang.com/users/profiles/minecraft/KrisJelbring"));
System.out.println("Name: "+node.get("name"));
System.out.println("Id: "+node.get("id"));
but if you don't like to use jackson you can do it by hand: but that's nonsense and not very stable:
while ((line = br.readLine()) != null)
{
int startOfId = line.indexOf("\"id\"") + 4;
int startOfValue = line.indexOf("\"", startOfId) + 1;
int endOfValue = line.indexOf("\"", startOfValue);
System.out.println("id: " + line.substring(startOfValue, endOfValue));
}
I'm trying to parse the first instance of "actualEPS" from the following JSON file:
{"symbol":"AAPL","earnings":[{"actualEPS":2.34,"consensusEPS":2.17,"estimatedEPS":2.17,"announceTime":"AMC","numberOfEstimates":10,"EPSSurpriseDollar":0.17,"EPSReportDate":"2018-07-31","fiscalPeriod":"Q3 2018","fiscalEndDate":"2018-06-30","yearAgo":1.67,"yearAgoChangePercent":0.40119760479041916,"estimatedChangePercent":0.29940119760479045,"symbolId":11},{"actualEPS":2.73,"consensusEPS":2.69,...}
Here is the method currently. I know I'm getting data from the target as I can Sysout the String "inputLine" and see the full file. I'm having trouble parsing from that point. I have installed and imported the org.JSON library.
public static void getData(String s) throws Exception {
String urlBase = new String(theWebSiteImGettingDataFrom)
URL targetURL = new URL(urlBase);
URLConnection yc = targetURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
} // end while
in.close();
JSONObject obj = new JSONObject(inputLine);
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
} // end getData method
I'm getting a Null Pointer Exception in the stack trace:
at org.json.JSONTokener.<init>(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:357)
at code.URLConnectionReader.getData(URLConnectionReader.java:39)
How do I parse the data for the first instance of "actualEPS" and only the first instance?
EDIT
JSONObject obj = new JSONObject(inputLine);
JSONArray earningsArray = obj.getJSONArray("earnings");
JSONObject firstEPS = earningsArray.getJSONObject(0);
double eps = firstEPS.getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
First of all, as I have no idea how big your json is and as you only want to parse a specific part of the json itself, I would recommend you to use JsonReader.class instead of JsonObject.class.
Short difference:
JsonObject parses the whole json into RAM and needs to be smaller than 1 MB.
JsonReader uses a streaming approach, which allows you to handle big jsons more efficiently.
Secondly, if you know that you ALWAYS just need the first instance of your json you could simply shorten your jsonString itself before parsing it (e.g. substring, etc.).
Now to your code:
public static void getData(String s) throws Exception {
String urlBase = new String(theWebSiteImGettingDataFrom); // Semicolon!
URL targetURL = new URL(urlBase);
URLConnection yc = targetURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
} // end while
in.close();
System.out.println("My json: "+inputLine); //TODO: What is your output here?
JSONObject obj = new JSONObject(inputLine);
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
} // end getData method
Please comment, as I can't tell if you get the json from your server.
EDIT:
Your error is in this line:
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
In short it should be like this:
double eps = obj.getJSONArray("earnings").getJSONObject(0).getDouble("actualEPS");
But, why? Your attribute earnings returns an JSONArray (Which means that you have multiple rows with indizes). So, instead of just requesting "earnings" as a JSONObject, you should rather use it as a JSONArray and then extract the first row (= .getJSONObject(0)). After extracting the first row you can actually use your double-Value.
I hope this works :)
2nd Edit:
Change that ..
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine); } // end while
to:
while ((inputLine += in.readLine()) != null) {
System.out.println(inputLine); } // end while
Your while loop keeps iterating until .readLine() returns null.
As null is the last iteration, you have only null in your variable.
As suggested you can solve that by simply changing the = to +=.
I hope we got it now. :)
I m trying to get an album information/album cover of an artist in my program.
i m trying to do it through (example: madonna/frozen);
String urlToRead = "http://www.musicbrainz.org/ws/2/recording/?query=artist:madonna+recording:frozen";
to get the album information and cover foto also if its possiable information about the artist.
what i habe been trying until now;
String urlToRead = "http://www.musicbrainz.org/ws/2/recording/?query=artist:madonna+recording:frozen";
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
String out = result;
But the output is a huge xml file in string format which full of information that doesnt really match what i want.(so many random albums which Madonna s song frozen in it)
Is there any other simple way to do it ? If not how could i get the exactly information from my output ?
Any tipps?
Have a look at this, if you haven't already: https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2/Search
That contains a list of fields that search fields you can use to narrow down your search result, including a LIMIT field if you only wish to retrieve one result.
As for the album art - that's handled by a separate API - https://musicbrainz.org/doc/Cover_Art_Archive/API - you can use their Java bindings in your program to fetch the album covers ( https://github.com/lastfm/coverartarchive-api )
Hello
I have a page of a personality in wikipedia and I want to extract with java source a code HTML from the main part is that.
Do you have any ideas?
Use Jsoup, specifically the selector syntax.
Document doc = Jsoup.parse(new URL("http://en.wikipedia.org/", 10000);
Elements interestingParts = doc.select("div.interestingClass");
//get the combined HTML fragments as a String
String selectedHtmlAsString = interestingParts.html();
//get all the links
Elements links = interestingParts.select("a[href]");
//filter the document to include certain tags only
Whitelist allowedTags = Whitelist.simpleText().addTags("blockquote","code", "p");
Cleaner cleaner = new Cleaner(allowedTags);
Document filteredDoc = cleaner.clean(doc);
It's a very useful API for parsing HTML pages and extracting the desired data.
For wikipedia there is API: http://www.mediawiki.org/wiki/API:Main_page
Analyze web page's structure
Use JSoup to parse HTML
Note that this returns a STRING (blob of a sort) of the HTML source code, not a nicely formatted content item.
I use this myself - a little snippet I have for whatever i need. Pass in the url, any start and stop text, or the boolean to get everything.
public static String getPage(
String url,
String booleanStart,
String booleanStop,
boolean getAll) throws Exception {
StringBuilder page = new StringBuilder();
URL iso3 = new URL(url);
URLConnection iso3conn = iso3.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
iso3conn.getInputStream()));
String inputLine;
if (getAll) {
while ((inputLine = in.readLine()) != null) {
page.append(inputLine);
}
} else {
boolean save = false;
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains(booleanStart))
save = true;
if (save)
page.append(inputLine);
if (save && inputLine.contains(booleanStop)) {
break;
}
}
}
in.close();
return page.toString();
}