如何在jsoup中选择此元素中的文本?

时间:2018-01-07 21:55:59

标签: html html5 jsoup

编辑:该网站为http://www.op.gg/statistics/champion/

我正在尝试使用此照片中以灰色突出显示的元素选择文字:https://i.gyazo.com/cc31794574692e2bc6709e193b27741c.png

我尝试了以下以及其他一些变体,但似乎都没有。

Document doc = Jsoup.connect("http://www.op.gg/statistics/champion/").get();
String s = "";
s += doc.select("table.StatisticsChampionTable.sortable.tablesorter.tablesorter-default 
.Content .Row.Top .Cell.ChampionName a").text();

3 个答案:

答案 0 :(得分:1)

尝试此操作可能会对您有所帮助。

您必须结合使用WebView和Jsoup来擦除数据。

首先,将网页加载到webview中,并使webview可见性不可见或消失。而不是将HTML字符串解析为JSoup。然后,您可以轻松地找到所需的所有标签,这里是根据您的要求的示例代码。

public class MainActivity extends AppCompatActivity {

    Handler handlerForJavascriptInterface = new Handler();
    private WebView mWebView;
    private String mURL = "http://www.op.gg/statistics/champion/";
    private String html_source;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = findViewById(R.id.wv);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new MyJavaScriptInterface(MainActivity.this), "HtmlViewer");
        mWebView.loadUrl(mURL);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // Page loading started
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                mWebView.loadUrl("javascript:window.HtmlViewer.showHTML" +
                        "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
            }
        });
    }

    class MyJavaScriptInterface {
        private Context ctx;

        MyJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        }

        @JavascriptInterface
        public void showHTML(final String html) {
            //code to use html content here
            handlerForJavascriptInterface.post(new Runnable() {
                @Override
                public void run() {
                    html_source = html;
                    new Description().execute();
                }
            });
        }
    }

    private class Description extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Connect to the web site
            Document mBlogDocument = Jsoup.parse(html_source);

            int mPaginationSize = mBlogDocument.select("td[class=Cell ChampionName]").size();

            for (int page = 0; page < mPaginationSize; page++) {

                String mChampionName = mBlogDocument.select("td[class=Cell ChampionName]").eq(page).text();

                Log.i("Champion Name " + page, mChampionName + "\n");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Set description into TextView
        }
    }
}

答案 1 :(得分:0)

尝试一下:

doc.select(".StatisticsChampionTable .ChampionName a").text()

也许this下次可以帮到你。

答案 2 :(得分:0)

import java.io.IOException;
import java.util.List;

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

public class Snippet {

    public static void main(String args[]) throws IOException, InterruptedException {

        Document doc = Jsoup.connect("http://www.op.gg/statistics/ajax2/champion/").get();

        List<Element> links = doc.select("table > tbody > tr > .Cell.ChampionName > a");
        for (Element link : links) {
            System.out.println(link.absUrl("href"));
        }

    }
}

样本输出:

http://www.op.gg/champion/Taric/
http://www.op.gg/champion/Quinn/
http://www.op.gg/champion/Kled/
http://www.op.gg/champion/Draven/
http://www.op.gg/champion/MonkeyKing/
http://www.op.gg/champion/Yorick/
http://www.op.gg/champion/Zilean/
http://www.op.gg/champion/Zyra/
http://www.op.gg/champion/Morgana/
http://www.op.gg/champion/Singed/
http://www.op.gg/champion/Nocturne/
http://www.op.gg/champion/Nami/
http://www.op.gg/champion/Udyr/