2016-08-07 14 views
0

私はこのウェブサイト(http://pokesniper.boosting-service.de/)の各列からポケモン名と座標を読み取って、そのようなデータを表示するデスクトップアプリケーションを開発しようとしています。私はHTMLでプログラミングしたことがありません。Java:JSoupを使用してこのWebサイトからデータを読み取るにはどうすればよいですか?そして、タグやクラスのようなものを見つけるにはどうすればいいですか?

私は多くのチュートリアルを検索しましたが、要素ID、タグなどをどのように取得してどのデータを印刷したいのかを決して説明しません。私はWebページの要素を調べることを知っていますが、探したいものがわからないので、JSoupの使い方を理解するために分析できるデータを出力する例を作って助けてください。大変感謝します。

あなたが気分が良ければ、タグとIDをどのように取得したかを説明することができますので、私はより早く学び、将来の参照のために知ることができます。

答えて

3

Jsoup、HTMLパーサは、その「jqueryの様」と「正規表現」セレクタの構文は非常に使いやすい、あなたが好きな取得するのに十分柔軟 です。 の下には、Jsoupを使用してリンク、画像、 ページタイトル、およびHTMLページの「div」要素の内容を取得する方法を示す3つの例があります。

基本的に、jsoupは、大量の編集が必要な場合にのみ使用してください。あなたが多くの<input>タグにIDを与えたい場合や、インターネットのウェブページからデータを読みたい場合は、

公式のjsoupウェブサイトのこの例が役立ちます。
https://jsoup.org/cookbook/extracting-data/example-list-links

このプログラム例は、URLからページをフェッチする方法を示しています。 リンク、画像、およびその他のポインタを抽出します。 URLと テキストを確認してください。

フェッチするURLをプログラムの唯一の引数として指定します。

package org.jsoup.examples; 

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

import java.io.IOException; 

/** 
* Example program to list links from a URL. 
*/ 
public class ListLinks { 
    public static void main(String[] args) throws IOException { 
     Validate.isTrue(args.length == 1, "usage: supply url to fetch"); 
     String url = args[0]; 
     print("Fetching %s...", url); 

     Document doc = Jsoup.connect(url).get(); 
     Elements links = doc.select("a[href]"); 
     Elements media = doc.select("[src]"); 
     Elements imports = doc.select("link[href]"); 

     print("\nMedia: (%d)", media.size()); 
     for (Element src : media) { 
      if (src.tagName().equals("img")) 
       print(" * %s: <%s> %sx%s (%s)", 
         src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), 
         trim(src.attr("alt"), 20)); 
      else 
       print(" * %s: <%s>", src.tagName(), src.attr("abs:src")); 
     } 

     print("\nImports: (%d)", imports.size()); 
     for (Element link : imports) { 
      print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel")); 
     } 

     print("\nLinks: (%d)", links.size()); 
     for (Element link : links) { 
      print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35)); 
     } 
    } 

    private static void print(String msg, Object... args) { 
     System.out.println(String.format(msg, args)); 
    } 

    private static String trim(String s, int width) { 
     if (s.length() > width) 
      return s.substring(0, width-1) + "."; 
     else 
      return s; 
    } 
} 

出力

Fetching http://news.ycombinator.com/... 

Media: (38) 
* img: <http://ycombinator.com/images/y18.gif> 18x18() 
* img: <http://ycombinator.com/images/s.gif> 10x1() 
* img: <http://ycombinator.com/images/grayarrow.gif> x() 
* img: <http://ycombinator.com/images/s.gif> 0x10() 
* script: <http://www.co2stats.com/propres.php?s=1138> 
* img: <http://ycombinator.com/images/s.gif> 15x1() 
* img: <http://ycombinator.com/images/hnsearch.png> x() 
* img: <http://ycombinator.com/images/s.gif> 25x1() 
* img: <http://mixpanel.com/site_media/images/mixpanel_partner_logo_borderless.gif> x (Analytics by Mixpan.) 

Imports: (2) 
* link <http://ycombinator.com/news.css> (stylesheet) 
* link <http://ycombinator.com/favicon.ico> (shortcut icon) 

Links: (141) 
* a: <http://ycombinator.com> () 
* a: <http://news.ycombinator.com/news> (Hacker News) 
* a: <http://news.ycombinator.com/newest> (new) 
* a: <http://news.ycombinator.com/newcomments> (comments) 
* a: <http://news.ycombinator.com/leaders> (leaders) 
* a: <http://news.ycombinator.com/jobs> (jobs) 
* a: <http://news.ycombinator.com/submit> (submit) 
* a: <http://news.ycombinator.com/x?fnid=JKhQjfU7gW> (login) 
* a: <http://news.ycombinator.com/vote?for=1094578&dir=up&whence=%6e%65%77%73> () 
* a: <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+readwriteweb+%28ReadWriteWeb%29&utm_content=Twitter> (Facebook speeds up PHP) 
* a: <http://news.ycombinator.com/user?id=mcxx> (mcxx) 
* a: <http://news.ycombinator.com/item?id=1094578> (9 comments) 
* a: <http://news.ycombinator.com/vote?for=1094649&dir=up&whence=%6e%65%77%73> () 
* a: <http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914> ("Tough. Django produces XHTML.") 
* a: <http://news.ycombinator.com/user?id=andybak> (andybak) 
* a: <http://news.ycombinator.com/item?id=1094649> (3 comments) 
* a: <http://news.ycombinator.com/vote?for=1093927&dir=up&whence=%6e%65%77%73> () 
* a: <http://news.ycombinator.com/x?fnid=p2sdPLE7Ce> (More) 
* a: <http://news.ycombinator.com/lists> (Lists) 
* a: <http://news.ycombinator.com/rss> (RSS) 
* a: <http://ycombinator.com/bookmarklet.html> (Bookmarklet) 
* a: <http://ycombinator.com/newsguidelines.html> (Guidelines) 
* a: <http://ycombinator.com/newsfaq.html> (FAQ) 
* a: <http://ycombinator.com/newsnews.html> (News News) 
* a: <http://news.ycombinator.com/item?id=363> (Feature Requests) 
* a: <http://ycombinator.com> (Y Combinator) 
* a: <http://ycombinator.com/w2010.html> (Apply) 
* a: <http://ycombinator.com/lib.html> (Library) 
* a: <http://www.webmynd.com/html/hackernews.html> () 
* a: <http://mixpanel.com/?from=yc> () 
関連する問題