I have a page link to extract some data from it (I want to get tables' some tds attributes).
I used for-loop to iteration via elements that I have to extract some attributes
of it . but I get duplicated output.
The output should be like the output at the image on the end of my post
Document doc = Jsoup.connect("http://www.saudisale.com/SS_a_mpg.aspx").get();
Elements elements = doc.select("table").select("tbody").select("tr").select("td") ;
for(Element e:elements) {
System.out.println(e.select("span[id~=Label4]").text() +
"\t" + e.select("input[id$=ImageButton1]").attr("src") +
"\t" + "" + e.select("span[id~=Label13]").text());
}
This is the output that I get them, they are duplicated!!! :
The output should be like this:-
Would you please try below code?
Elements description = doc.select("tbody");
doc=Jsoup.parse(description.html());
description = doc.select("td");
for(int j = 0; j < description.size(); ++ j)
{
String bodytext = description.eq(j).text(); // bodytext is the text of each TD
}
I used for-loop with incrementing counter instead and problem solved.
where 31 is the number of items on that page
The following code gives the desired output.
for(int i=1;i<description.size();i++)
{
System.out.println(elements.select("td").select("span[id~=Label4]").get(i).text()+""+elements.select("td").select("input[id$=ImageButton1]").get(i).attr("src"));
}
Related
I need to return json array each element in new line. but instead it's printing in one line. I tried using '/n' and space but it's not working.
String json = null;
//split cookie with delimiter to store in array
String wl[] = wishList.split("~");
//JSON RETURN VALUE
json = "[\"";
for(int i = 0; i < wl.length; ++i) {
json += wl[i];
//tried but didn't work
//json.split("\n");
}
json += "\"]";
System.out.println(json);
so i tried cancatinating in for loop but it didn't work.
I need out like this
1.abc
2.bcd
3.efg
but i'm getting this output
1.abcbcdefg
What you are trying to do (if I get it right) is to split a String on the ~ and creating a JSON array with it (one separate line).
Did you thought about simply replacing the character ?
String json = "[" + wishlist.replace("~", ",\n") + "]"; //Added a , to separated each elements
But if you want to do it yourself,
String json = "[";
for(int i = 0; i < wl.length; ++i) {
json += wl[i] + ",\n";
}
json += "]";
You need to add the newLine character, not split it again.
Off course, as pointed, this should be done using a Library design to do it but here is a simple correction of you code.
PS : I didn't write the needed \" for redability. But this can be easily added in both solutions.
I tried using '/n' and space but it's not working.
You should use "\n" in your JSON String to add new line and not "/n", it is not the same thing. You should do the same thing for the regex pattern.
3. Line Separator is '\n'
This means '\r\n' is also supported because trailing white space is ignored >when parsing JSON values.
The last character in the file may be a line separator, and it will be
treated the same as if there was no line separator present.
Source : http://jsonlines.org/
Edit
A Json array has comma to separate values of the array and string should be between quotes or double quotes. but doesn't need "\" at the begining and at the end.
You could get this String :
["abc",
"bcd",
"efg"]
with this code :
final String STRING_DOUBLE_QUOTE="\"";
String json = null;
// split cookie with delimiter to store in array
String wl[] = wishList.split("~");
// JSON RETURN VALUE
json = "[";
for (int i = 0; i < wl.length; ++i) {
if (!json.equals("[")) {
json += ",\n";
}
json += STRING_DOUBLE_QUOTE + wl[i] + STRING_DOUBLE_QUOTE;
}
json += "]";
System.out.println(json);
If you want it to be on multiple lines, you're gonna have to save it in multiple elements i.e. an array.
Try this:
String wl[] = wishList.split("~");
for(int i = 0; i < wl.length; ++i){
System.out.println(wl[i]);
}
Unsure if this is what you're asking for, but this is the answer to your written question
I need to make a program that let's you add CD titles, remove them etc.
I need to use an Arraylist for my program (to store the songs)
Code:
ArrayList songlist = new ArrayList();
Collections.addAll(songlist, "Something", "Hard Days Night", "I am the Walrus", "Yesterday", "All the Lonely People");
Collections.sort(songlist);
int songlistsize = songlist.size ();
for (int i = 0; i < songlistsize; i++) {
outputField.setText(i + ": " + songlist.get(i));
The problem is that the program will only display "Yesterday", and not anything else.
outputField.setText(i + ": " + songlist.get(i));
Because you are setting the last value and not appending. Do something like this:
StringBuilder string = new StringBuilder();
for (int i = 0; i < songlistsize; i++) {
string.append(songlist.get(i));
}
outputField.setText(string);
There are many other problems with the code but I am sticking to the point.
If you try to print your output on the console you will see that the part that deals with the collection works fine.
But since setText() replaces the current String with the latest song name you only see "Yesterday" because its at the end of your collection.
That´s why you should try to append() the next song name to your String or make sure you copy your current String, add the next item and finally use setText()
For example:
String string = "";
for (int i = 0; i < songlistsize; i++)
{
string = outputField.getText() + songlist.get(i);
outputField.setText(string);
}
I was trying to concatenate a string to itself + something else, like this:
String example = " "
for (int i = 0; i < 5; i++) {
if (condition OK) {
example = example + "\nAnother text";
}
}
JOptionPane.showMessageDialog(null, example);
In my mind, it should've print " (new line)Another text" but it seems to work only with the last entry in my "Another text". Like, if the condition inside the "for" loop is OK 3 times, it prints " (new line)Another text(3)" instead of " (new line) Another Text(1) (new line) Another text(2)...
Any idea of what may be happening?
EDIT: after realizing that my code was fine, I followed afzalex recommendation and found out the error was in my condition. Thanks bro
I used below program I got expected output.
String example = " ";
for (int i = 0; i < 5; i++) {
if (i == 1 || i == 3) {
example = example + "\nAnother text";
}
}
System.out.println(example);
Output:
Another text
Another text
So, probably it could be something wrong with JOptionPane.showMessageDialog(null, example); If it is being interpreted as HTML in the end, then better use </br> instead of \n, that can give you new line.
I've been trying to get data from: http://www.betvictor.com/sports/en/to-lead-anytime, where I would like to get the list of matches using JSoup.
For example:
Caen v AS Saint Etienne
Celtic v Rangers
and so on...
My current code is:
String couponPage = "http://www.betvictor.com/sports/en/to-lead-anytime";
Document doc1 = Jsoup.connect(couponPage).get();
String match = doc1.select("#coupon_143751140 > table:nth-child(3) > tbody > tr:nth-child(2) > td.event_description").text();
System.out.println("match:" + match);
Once I can figure out how to get one item of data, I will put it in a for loop to loop through the whole table, but first I need to get one item of data.
Currently, the output is "match: " so it looks like the "match" variable is empty.
Any help is most appreciated,
I have worked out how to answer my question after a few hours of experimenting. Turns out the page didn't load properly straight away, and had to implement the "timeout" method.
Document doc;
try {
// need http protocol
doc = Jsoup.connect("http://www.betvictor.com/sports/en/football/coupons/100/0/0/43438/0/100/0/0/0/0/1").timeout(10000).get();
// get all links
Elements matches = doc.select("td.event_description a");
Elements odds = doc.select("td.event_description a");
for (Element match : matches) {
// get the value from href attribute
String matchEvent = match.text();
String[] parts = matchEvent.split(" v ");
String team1 = parts[0];
String team2 = parts[1];
System.out.println("text : " + team1 + " v " + team2);
}
I am attempting to convert an html page with entries that have multiple types of details (e.g. name, phone number, and address), into a spreadsheet. I am able to to isolate each of these details as Elements, but I cannot seem to find a way to iterate over multiple Elements at once to print names and phone numbers next to one another rather than having all the names printed and then all of the phone numbers printed.
Jsoup.connect(page).timeout(999999);
Document doc = Jsoup.connect(page).get();
String title = doc.title();
System.out.println(title);
Elements names = doc.select("li a");
Elements ratings = doc.select("li img");
for (Element name:names){
if (name.attr("href").startsWith("/biz/")){
System.out.println(name.text());
}
for (Element rating:ratings){
System.out.println(rating.attr("alt"));
}
Assuming the index its the same for both this would work fine.
for(int i = 0; i < names.size() && i < ratings.size(); i++) {
System.out.println("Name: " + names.get(i) + " Phone: " + ratings.get(i));
}