Android:如何解析 HTML 站点并仅获取一些特定数据?

发布于 2024-12-11 23:04:55 字数 358 浏览 0 评论 0原文

我有一个问题:

我有一个链接:http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6) 我只想从此链接中获取一些特定数据并显示在 Android 的文本视图中。

这在Android中可能吗,我的意思是有没有机会通过解析或者我不知道,你们可以建议我。

例如,我只想从该网站获取此专栏 Nästa tur (min)。

问候

I have a question:

I have a link: http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)
and I wanna take only some specific data from this link and to show in textview in Android.

Is this possible in Android, I mean is there any chance by parsing or I don't know, you can suggest me guys.

For example I just want to take this column Nästa tur (min) from that site.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-12-18 23:04:55

JSoup 非常好并且越来越流行。以下是解析整个表的方法:

URL url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm");
Document doc = Jsoup.parse(url, 3000);

Element table = doc.select("table[title=Avgångar:]").first();

Iterator<Element> it = table.select("td").iterator();

//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
  // do what ever you want with the td element here

  //iterate three times to get to the next td you want. checking after the first
  // one to make sure
  // we're not at the end of the table.
  it.next();
  if(!it.hasNext()){ 
    break;
  }
  it.next();
  it.next();
}

JSoup is pretty nice and getting popular. Here's how you could just parse the whole table:

URL url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm");
Document doc = Jsoup.parse(url, 3000);

Element table = doc.select("table[title=Avgångar:]").first();

Iterator<Element> it = table.select("td").iterator();

//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
  // do what ever you want with the td element here

  //iterate three times to get to the next td you want. checking after the first
  // one to make sure
  // we're not at the end of the table.
  it.next();
  if(!it.hasNext()){ 
    break;
  }
  it.next();
  it.next();
}
优雅的叶子 2024-12-18 23:04:55

如果解析看起来足够简单并且您希望也可以使用正则表达式来查找 html 的正确部分。无论如何,了解正则表达式在某些时候还是很有用的。使用一些 XML/HTML 解析库是更灵活的方法(例如 XMLReader)。

If the parsing seems simple enough and you want you could also use regular expressions to find the correct part of the html. Regular expressions will be useful to know at some point anyway. Using some XML/HTML parsing library is the more flexible way to do it (XMLReader for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文