Android 通过 WebView 无头浏览? [英] Android Headless Browsing through WebView?

查看:43
本文介绍了Android 通过 WebView 无头浏览?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个网站创建一个 Android 应用程序,这不是我的.但它是餐厅的搜索引擎.他们没有可以使用的 API.我想漫不经心地浏览他们的网站并将搜索查询放在 HTML 表单中,然后单击提交按钮.然后解析结果并将其与我的应用程序代码一起使用.在这里做了大量的研究之后,我终于要求它.问题 1问题 2问题 3 以及我目前看过的更多内容.所以到目前为止我所知道的是,如果我想在 Google.com 上做同样的事情,我会写:

I am trying to create an Android app for a Website, Which is not mine. But is a search engine for Restaurants. They have no API to work with. And i want to heedlessly browse their website and put the search query in the HTML Form and Click the Submit Button. And then Parse the Results and Use it with my Application Code. After doing loads of research here, i am finally asking for it. Question 1, Question 2, Question 3 and many more that i have looked so far. So all i know so far is if i want to do the same on Google.com i would write:

myWebView.getSettings().setJavaScriptEnabled(true);

myWebView.loadUrl("http://www.google.com/");

myWebView.setWebViewClient(new WebViewClient() {

@Override
public void onPageFinished(WebView view, String url) {
       //Load HTML
            myWebView.loadUrl("javascript:document.getElementById('q') =" + "StackOverFlow" + "; document.getElementByName('btnK').click();");
        }
    });

在上面的代码中,我试图输入搜索词StackOverFlow"并单击搜索"按钮.但它不起作用.请在此代码中帮助我,或者为我指明正确的方向.

In the above code i am trying to put the search term "StackOverFlow" and Click the Search Button. But its not working. Kindly Help me out in this code or either point me in the right direction.

推荐答案

已经有一段时间了,但是为了让其他人知道,webviews 不再使用 loadUrl 来运行 Javascript.尝试使用 evaluateJavascript.

It's been a while, but for the sake of letting others know, webviews no longer use loadUrl to run Javascript. Try using evaluateJavascript.

由于您还提到了无头浏览,我建议您在客户端中覆盖 shouldInterceptRequest 以将所有不必要的文件(例如 css、图像,可能还有 js,具体取决于站点)重定向到空白输入流

Since you've also mentioned headless browsing, I would recommend overriding shouldInterceptRequest in your client to redirect all unnecessary files (such as css, images, and perhaps js depending on the site) to a blank inputstream

这篇关于Android 通过 WebView 无头浏览?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆