在Android中抓取HTML网页的最快方法是什么? [英] What is the fastest way to scrape HTML webpage in Android?

查看:37
本文介绍了在Android中抓取HTML网页的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 Android 中的非结构化网页中提取信息.我想要的信息嵌入在没有 id 的表格中.

I need to extract information from an unstructured web page in Android. The information I want is embedded in a table that doesn't have an id.

<table> 
<tr><td>Description</td><td></td><td>I want this field next to the description cell</td></tr> 
</table>

我应该使用

  • 模式匹配?
  • 使用 BufferedReader 提取信息?

或者有没有更快的方法来获取这些信息?

Or are there faster way to get that information?

推荐答案

我认为在这种情况下,寻找一种快速方式来提取信息是没有意义的因为当您将其与下载 HTML 所需的时间进行比较时,答案中已经建议的方法之间几乎没有性能差异.

I think in this case it makes no sense to look for a fast way to extract the information as there is virtually no performance difference between the methods already suggested in answers when you compare it to the time it will take to download the HTML.

因此假设 最快 是指最方便、可读和可维护的代码,我建议您使用 DocumentBuilder 使用 XPathExpressions:

So assuming that by fastest you mean most convenient, readable and maintainable code, I suggest you use a DocumentBuilder to parse the relevant HTML and extract data using XPathExpressions:

Document doc = DocumentBuilderFactory.newInstance()
  .newDocumentBuilder().parse(new InputSource(new StringReader(html)));

XPathExpression xpath = XPathFactory.newInstance()
  .newXPath().compile("//td[text()="Description"]/following-sibling::td[2]");

String result = (String) xpath.evaluate(doc, XPathConstants.STRING);

如果您碰巧检索到无效的 HTML,我建议隔离相关部分(例如使用 substring(indexOf("<table")..) 并在必要时使用 <更正剩余的 HTML 错误解析之前的代码>字符串操作.但是,如果这变得太复杂(即非常糟糕 HTML),只需按照其他答案中的建议使用hacky模式匹配方法.

If you happen to retrieve invalid HTML, I recommend to isolate the relevant portion (e.g. using substring(indexOf("<table")..) and if necessary correct remaining HTML errors with String operations before parsing. If this gets too complex however (i.e. very bad HTML), just go with the hacky pattern matching approach as suggested in other answers.

备注

  • XPath 从 API 级别 8 (Android 2.2) 开始可用.如果您针对较低的 API 级别进行开发,则可以使用 DOM 方法和条件来导航到要提取的节点

这篇关于在Android中抓取HTML网页的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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