简体   繁体   English

如何从HTML WebView中的标签获取信息?

[英]How I can get information from a tag in HTML WebView?

I would like to know how I can get a information inside a tag in HTML. 我想知道如何在HTML的标签内获取信息。 I don't know if I am doing it right because It don't return any information. 我不知道自己是否正确,因为它不返回任何信息。 I show you my android code to see if you can help me. 我给你看我的android代码,看看你能不能帮到我。

code class: 代码类:

    public class WebView1 extends Activity {
        /** Called when the activity is first created. */   
WebView browse;         
        @Override
        protected void onCreate(Bundle savedInstanceState)      {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.webview1);
         browse = (WebView) findViewById(R.id.webview1);                     

         browse.setWebChromeClient(new WebChromeClient());

         browse.setWebViewClient(new WebViewClient() {
                @Override  
                public void onPageFinished(WebView view, String url) {
                    File input = new File("file:///android_asset/ejemploWebview.html");
                    Document doc = null;
                    try {
                        doc = Jsoup.parse(input, "UTF-8");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    //http://jsoup.org/cookbook/input/load-document-from-url
                    //Document doc = Jsoup.connect("http://example.com/").get();

                    Element content = doc.getElementById("div");
                    Elements links = content.getElementsByTag("id");
                    String linkId = links.attr("manolo");
                    System.out.print(linkId); //I need that it return Hiiii!
                    }
            });     } }

code HTML: 代码HTML:

 <html>
    <head>
    </head>
    <div id="james">hellooo!</div>
    <div id="paco">byeee!</div>
    <div id="manolo">Hiii!</div>
    </html>

I hope I explained correctly! 希望我能正确解释! Thank's you! 谢谢! ;) ;)

Personally, I've always had better results from JSoup when using the selectors, as detailed here . 就个人而言,使用选择的时候我一直有从JSoup更好的结果,详见这里

From the example you give, it appears you want to retrieve the div values by their ID, you can use this : 在给出的示例中,您似乎想要按ID检索div值,可以使用以下方法:

el#id: elements with ID, eg div#logo el#id:具有ID的元素,例如div#logo

So either use 3 occurrences of the above, or just select the divs and iterate over them doing whatever you need to do. 因此,可以使用上述3次出现的结果,或者只是选择div并对其进行迭代以执行所需的任何操作。

Hope this helps. 希望这可以帮助。

PS, easiest thing I found was to put a breakpoint after you call doc = Jsoup.parse(input, "UTF-8"); PS,我发现最简单的方法是在调用doc = Jsoup.parse(input, "UTF-8");之后放置一个断点doc = Jsoup.parse(input, "UTF-8"); , then use your IDEs' expression builder to work out which selector does what you want :) ,然后使用IDE的表达式生成器来确定哪个选择器可以完成您想要的操作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM