2017-04-11 39 views
-1

我想拉扯使用j湯的reddit源代碼的用戶名,然後試圖DM他們,但我不能圖我們如何拉鍊接說/用戶。對不起,這真的很混亂。試圖從使用Jsoup從Reddit的用戶名

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

import java.io.IOException; 

public class Main 
{ 

public static void main(String[] args) { 

    Document doc; 
    try { 

     // need http protocol 
     doc = Jsoup.connect("https://www.reddit.com/new/).get(); 

     // get page title 
     String title = doc.title(); 
     System.out.println("title : " + title); 

     // get all links 
     Document doc = Jsoup.parse("https://www.reddit.com/new"); 
     Elements certainLinks = 
     doc.select("https://www.reddit.com/user"); { 

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

      } 


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

     } 

    } 
    } 
+1

什麼是你的問題? – tnw

+1

聽起來像垃圾郵件給我 – efekctive

回答

1

水木清華這樣

 Document doc = Jsoup.connect("https://www.reddit.com/new/").get(); 
     Elements certainLinks = doc.select("a[href*=https://www.reddit.com/user/]"); 
     certainLinks.forEach(l -> System.out.println(l.text())); 

會打印:

_serial_chiller 
dracorian 
ImagesOfNetwork 
... 

a[href*=https://www.reddit.com/user/]意味着所有a元素包含href屬性https://www.reddit.com/user/