JSoup:如何解析特定链接

时间:2016-03-12 10:24:35

标签: java android html jsoup

我正在构建一个Android应用程序并且我试图只从以下网站获取特定链接,但我不能,因为该网站对所有类使用相同的名称(这只是一个小的部分来自网站的HTML代码。)

<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>

如何使用id =&#34; JOT_FILECAB_label_wuid解析:gx:4c83ae813389c090&#34;?

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方式

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

        }
}

答案 1 :(得分:0)

我找到了一个解决我问题的简单解决方案。我选择所有&#34; href&#34;从网站上,我将元素存储在一个数组中,然后从数组中选择我想要的那个。

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();
  }


 }
};

感谢您的帮助。