2016-09-10 132 views
1

我想使用java获取此图片中的链接,图像位于下方。该网页中的链接更少。我发现这个代码在stackoverflow,我不明白如何使用它。[JAVA]从网页获取HTML链接

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class weber{ 
    public static void main(String[] args)throws Exception{ 
     String url = "http://www.skyovnis.com/category/ufology/"; 
     Document doc = Jsoup.connect(url).get(); 

     /*String question = doc.select("#site-inner").text(); 
     System.out.println("Question: " + question);*/ 

     Elements anser = doc.select("#container .entry-title a"); 
     for (Element anse : anser){ 
      System.out.println("Answer: " + anse.text()); 
     } 
    } 
} 

代码是从我找到的原始编辑。请帮忙。

image

回答

1

对于您的URL以下代码工作正常。

public static void main(String[] args) { 

    Document doc; 
    try { 

     // need http protocol 
     doc = Jsoup.connect("http://www.skyovnis.com/category/ufology/").userAgent("Mozilla").get(); 
     // get page title 
     String title = doc.title(); 
     System.out.println("title : " + title); 

     // get all links (this is what you want) 
     Elements links = doc.select("a[href]"); 
     for (Element link : links) { 

      // get the value from href attribute 
      System.out.println("\nlink : " + link.attr("href")); 
      System.out.println("text : " + link.text()); 

     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    } 

产量

title : Ufology 

link : http://www.shop.skyovnis.com/ 
text : Shop 

link : http://www.shop.skyovnis.com/product-category/books/ 
text : Books 

下面的代码过滤器通过它的文本链接。

 for (Element link : links) { 



      if(link.text().contains("Arecibo Message"))//find the link with some texts 
      { 
       System.out.println("here is the element you need"); 
       System.out.println("\nlink : " + link.attr("href")); 
       System.out.println("text : " + link.text()); 
      } 


     } 

建议在Jsoup中指定一个“userAgent”,以避免HTTP 403错误消息。

Document doc = Jsoup.connect(“http://anyurl.com”).userAgent(“Mozilla”)。get();

“Onna malli mage yuthukama kala。”

refernce:

https://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/

+0

非常感谢MCN。 – Aimkiller