2016-07-28 13 views
1

JavaでjSoupライブラリを使用してthis linkからぼかしています。私の情報源はうまくいっていますし、私が得たすべての要素をどのように分割するのか尋ねたいのですが?その結果、ここでスプリットアップjSoupスクレイピング結果

私のソース

package javaapplication1; 

import java.io.IOException; 
import java.sql.SQLException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

public class coba { 

    public static void main(String[] args) throws SQLException { 
    MasukDB db=new MasukDB();   
     try { 
      Document doc = null; 
      for (int page = 1; page < 2; page++) { 
       doc = Jsoup.connect("http://hackaday.com/page/" + page).get(); 
       System.out.println("title : " + doc.select(".entry-title>a").text() + "\n"); 
       System.out.println("link : " + doc.select(".entry-title>a").attr("href") + "\n"); 
       System.out.println("body : " + String.join("", doc.select(".entry-content p").text()) + "\n"); 
       System.out.println("date : " + doc.select(".entry-date>a").text() + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

、ウェブサイトのすべてのページには、みんなそれを分割する方法を、1行になりますか?そしてすべての記事にリンクを取得する方法を、私は おかげで、そこからあなたが彼らのテキストをこするされ、ドキュメント全体を検索し、リンクのリストを返します

答えて

0
doc.select(".entry-title>a").text() 

これを交配リンク側の私のCSSセレクタがまだ間違っていると思いますノード。しかし、あなたはおそらくすべての記事を掻き集め、それぞれから適切なデータを取得したいと考えています。

Document doc; 
    for (int page = 1; page < 2; page++) { 

     doc = Jsoup.connect("http://hackaday.com/page/" + page).get(); 

     // get a list of articles on page 
     Elements articles = doc.select("main#main article"); 

     // iterate article list 
     for (Element article : articles) { 

      // find the article header, which includes title and date 
      Element header = article.select("header.entry-header").first(); 

      // find and scrape title/link from header 
      Element headerTitle = header.select("h1.entry-title > a").first(); 
      String title = headerTitle.text(); 
      String link = headerTitle.attr("href"); 

      // find and scrape date from header 
      String date = header.select("div.entry-meta > span.entry-date > a").text(); 

      // find and scrape every paragraph in the article content 
      // you probably will want to further refine the logic here 
      // there may be paragraphs you don't want to include 
      String body = article.select("div.entry-content p").text(); 

      // view results 
      System.out.println(
        MessageFormat.format(
          "title={0} link={1} date={2} body={3}", 
          title, link, date, body)); 
     } 
    } 

この種のデータをスクラップする方法の例については、CSS Selectorsを参照してください。

+0

おかげさまで、あなたのスクリプトはとてもうまく動作しています。私は賢明です。 Pythonを使った私の治療とほぼ同じです:Dもう一度ありがとう – jethow

関連する問題