2017-04-11 42 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/