简体   繁体   English

从WebSite获取HTML代码并删除不必要的代码-仅在WebView中显示表格

[英]Get HTML Code from WebSite and remove unnecessary Code - Only display the Table in WebView

I am trying to get my app to sync itself with a website, and display only the table of a website. 我正在尝试让我的应用与网站同步,并仅显示网站表格。 Example: Normal HTML framework, and the table is inside a div#table, so I did it pretty simple and built the HTML Framework: 示例:普通的HTML框架,并且该表位于div#table内,因此我做起来非常简单,并构建了HTML框架:

String html = "<html> " +
        "<head>" +
        "</head>" +
        "<body style=\"background-color: transparent\">" +
        "?body" +
        "</body>" +
        "</html>";

And then I tried to replace the ?body with the table, but here's my problem: I have no clue how to do that and then load it into a transparent WebView. 然后,我尝试用表替换?body,但这是我的问题:我不知道如何执行此操作,然后将其加载到透明的WebView中。 I found a little bit code online, and tried to get it working for my case, so here's that: 我在网上找到了一些代码,并尝试使其适用于我的情况,所以这就是:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    List<String> links = new ArrayList();
    setContentView(SliderCreator.createSlider(R.layout.activity_news, this));
    wv = (WebView) findViewById(R.id.newsWebView);
    wv.setBackgroundColor(Color.TRANSPARENT);
    try {
        String finalHTML = getIntent().getStringExtra("html");
        if (!finalHTML.contains("<html>")) {
            String css = "";
            for(String link : links) {
                css+=Utils.getHTML(link);
            }
            finalHTML = html.replace("?body", getIntent().getStringExtra("html")).replace("?css", css);
        }
        wv.getSettings().setJavaScriptEnabled(true);
        //open URLs in external Browser
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
        });
        wv.loadDataWithBaseURL("", finalHTML, "text/html", "UTF-8", "");
    } catch(Exception e) {
        Toast.makeText(this, "An error occured...", Toast.LENGTH_LONG).show();
    }

But that doesn't work.. Can somebody help me out get that working for me? 但这是行不通的。有人可以帮我解决这个问题吗? (Btw.: I tried it with RegEx, didn't work, I tried it with JSoup, didn't work either...) (顺便说一句:我用RegEx尝试过,没用,我用JSoup尝试过,也没用...)

try using HTML Agility Pack if not working with RegEx 如果不适用于RegEx,请尝试使用HTML Agility Pack

Please note , your RegEx might not work the desired way if the text contains nested table tags 请注意 ,如果文本包含嵌套表标签,则RegEx可能无法正常工作

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

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