Jsoup:how To Parse A Specific Link
I'm building an android app and I 'm trying to get only a specific link,from the following site but I cannot, because the site uses the same name for all classes (this only a small
Solution 1:
I found a simple solution, which solves my problem. I select all the "href" from the site, I store the elements in an array, and from array I choose the one I want.
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
publicclassMain {
publicstaticvoidmain(String[] args) {
Documentdoc=null;
try {
doc = Jsoup.connect("https://sites.google.com/site/aenmakmech/tmemata").get();
Elementslinks= doc.select("a[href]");
String[] urls = newString[links.size()];
for (inti=0; i < links.size(); i++) {
urls[i] = links.get(i).attr("href");
//System.out.println(prices[i]);
}
Stringspecific_url= urls[77];
System.out.print(specific_url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Thank's for your help.
Solution 2:
You can try as following way
Document doc;
Element table;
Elements rows;
table = doc.select("table").get(0); // select the first table.
rows = table.select("tr");
for (int i = 0; i < rows.size(); i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
// Elements links = table.getElementsByTag("a");// Elements heading = cols.select("h3");if (cols.size() > 0)
if (cols.get(0).text().contains("Football")) {
String name = cols.get(0).text();
String type = cols.get(1).text();
String myLink = cols.get(2).html();
String parseLink = myLink.substring(myLink.indexOf("\"") + 1, myLink.lastIndexOf("\""));
String newLink = parseLink.replaceAll("&", "&");
}
}
Post a Comment for "Jsoup:how To Parse A Specific Link"