JSoup:How to parse a specific link

送分小仙女□ 提交于 2020-01-17 05:11:11

问题


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 part from the site's HTML code).

<td class="td-file"><span class="td-value"  
id="JOT_FILECAB_label_wuid:gx:4c83ae813389c090" aria-hidden="true">
Ε.ΛΣΧ.ΑΕΝ.02 ΑΠΟ 22-2-2016.pdf</span><br />
<SPAN style="word-spacing: 3px;">
<a href="https://docs.google.com/viewer?a=v&amp;pid=sites&amp;srcid=ZGVmYXVsdGRvbWFpbnxhZW5tYWttZWNofGd4OjRjODNhZTgxMzM4OWMwOTA" dir="ltr" target="_blank">Προβολή</a> 
<a href="/site/aenmakmech/tmemata/%CE%95.%CE%9B%CE%A3%CE%A7.%CE%91%CE%95%CE%9D.02%20%CE%91%CE%A0%CE%9F%2022-2-2016.pdf?attredirects=0&amp;d=1" dir="ltr">Λήψη</a>
</SPAN></td>

How to parse using id="JOT_FILECAB_label_wuid:gx:4c83ae813389c090"?


回答1:


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("&amp;", "&");

        }
}



回答2:


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;

public class Main {

 public static void main(String[] args) {
  Document doc = null;

  try {
   doc = Jsoup.connect("https://sites.google.com/site/aenmakmech/tmemata").get();
   Elements links = doc.select("a[href]");

   String[] urls = new String[links.size()];
   for (int i = 0; i < links.size(); i++) {
    urls[i] = links.get(i).attr("href");
    //System.out.println(prices[i]);
   }

   String specific_url = urls[77];
   System.out.print(specific_url);

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }


 }
};

Thank's for your help.



来源:https://stackoverflow.com/questions/35956439/jsouphow-to-parse-a-specific-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!