Android的网络与无头的浏览器刮 [英] Android Web Scraping with a Headless Browser

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

问题描述

我已经花了一天时间在研究可用于完成以下库:

I have spent a day on researching a library that can be used to accomplish the following:

  • 检索喜欢的背景网页的全部内容,而不渲染结果的视图。
  • 的LIB应该支持触发了Ajax请求的页面加载一些额外的结果数据后最初的HTML已加载的例子。
  • 从生成的HTML我需要抓住的XPath或CSS选择器表单元素。
  • 在今后我也可能需要导航到下一个页面(脱火事件,提交按钮/链接等)

下面是我曾尝试没有成功:

Here is what I have tried without success:

  • Jsoup:伟大的作品,但对JavaScript / AJAX不支持(所以它不会加载整个页面)
  • Android的内置HttpEntity:同样的问题,用JavaScript / AJAX为jsoup
  • 的HtmlUnit:看起来正是我需要的,但下班后不能得到它试图加载12MB +价值jar文件在Android上工作(没有其他的用户我自己装的全部源$ C ​​$ c和引用它作为一个。项目库才发现的东西,如小程序和java.awt中(通过使用的HtmlUnit)不会在Android中存在的话)。
  • 犀牛 - 我觉得这很混乱,不知道如何得到它在Android的工作,哪怕是我所期待的
  • 硒司机:看起来像它可以工作,但你没有实现它在无头的方式,让你不必显示视图中的实际HTML一个简单的方法

我真的想给的HtmlUnit工作,因为它似乎是最适合我的解决方案。有什么办法或至少另一个库我已经错过了,是适合我的需要?

I really want HtmlUnit to work as it seems the best suited for my solution. Is there any way or at least another library I have missed that is suitable for my needs?

我目前使用的是Android 0.1.7工作室上午可以移动,如果需要的椭圆。

I am currently using Android Studio 0.1.7 and can move to Ellipse if needed.

在此先感谢!

推荐答案

确定2周后我不服输,并使用一种解决方法这对我来说,目前的伟大工程。

Ok after 2 weeks I admit defeat and are using a workaround which works great for me at the moment.

问题:
它是太难端口的HtmlUnit到Android(或至少是与我的专业知识水平)。我相信它是一个有价值的项目(而不是费时经验的Java程序员)。我通过电子邮件发送的家伙的HtmlUnit,他们评论说,他们没有寻找到一个端口或什么努力都将参与,但建议任何人谁愿意开始这样的项目应该发送一个消息,他们的邮件列表,涉足更多的开发者(<一HREF =htt​​p://htmlunit.sourceforge.net/mail-lists.html相对=nofollow> http://htmlunit.sourceforge.net/mail-lists.html )。

The problem:
It is too difficult to port HTMLUnit to Android (or at least with my level of expertise). I am sure its a worthwhile project (and not that time consuming for experienced java programmer) . I emailed the guys at HTMLUnit and they commented that they are not looking into a port or what effort will be involved but suggested anyone who wants to start with such a project should send an message to their mailing list to get more developers involved (http://htmlunit.sourceforge.net/mail-lists.html).

解决方法:
我用内置的web视图Android的和overrided web视图类的onPageFinished方法注入Javascript脚本,抓住一切后,页面完全加载的HTML。 web视图还可以用于对称为futher JavaScript动作,点击按钮,填写表格等

The workaround:
I used android's built in WebView and overrided the onPageFinished method of Webview class to inject Javascript that grabs all the html after the page has fully loaded. Webview can also be used to called futher javascript actions, clicking buttons, filling in forms etc.

code:

Code:

webView.getSettings().setJavaScriptEnabled(true);
MyJavaScriptInterface jInterface = new MyJavaScriptInterface(context);
webView.addJavascriptInterface(jInterface, "HtmlViewer");

webView.setWebViewClient(new WebViewClient() {

@Override
public void onPageFinished(WebView view, String url) {

   //Load HTML
   webView.loadUrl("javascript:window.HtmlViewer.showHTML
       ('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}

webView.loadUrl(StartURL);
ParseHtml(jInterface.html);   

public class MyJavaScriptInterface {

    private Context ctx;
    public String html;

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

    @JavascriptInterface
    public void showHTML(String _html) {
        html = _html;
    }
}

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

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