如何在 android studio 中使用 jsoup 解析这个 span id 元素?
how to get parse this span id element with jsoup in android studio?
我想在我的 android 工作室项目中从这个 url 解析 span id = shd2b,
https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html
Screenshot in link below
我使用下面的 java 代码来解析它并显示在文本视图框中。
@Override
protected Void doInBackground(Void... voids) {
//Connect to the website
String url = "https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html";
//Get the title of the website
Document doc = null;
doc = (Document) Jsoup.parse(url);
eurotoinr = doc.getElementById("span.shd2b");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
textView.setText((CharSequence) eurotoinr);
}
Bu 当我 运行 时,模拟器设备中没有显示任何内容。有人可以帮我解决这个问题吗?
如果我描述的不够清楚或者看不懂,还请见谅。我只是在这次封锁期间才开始学习 android studio,并且对 stack overflow 很陌生。
我看到两个错误。
- 您是否尝试过调试或显示文档在解析后的外观?它看起来像这样:
<html>
<head></head>
<body>
https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html
</body>
</html>
因为你使用了错误的方法,你的字符串被解析为 HTML 片段。它不会尝试下载任何东西。
你应该使用
doc = Jsoup.connect(url).get();
然后它下载 HTML 并正确解析。
- 您的代码
doc.getElementById("span.shd2b");
将 return 为空,因为 span.shd2b
不是有效 ID。 ID 是 shd2b;
使用 doc.getElementById("shd2b;");
并选择预期的元素:
<span id="shd2b;">82.9337</span>
我想在我的 android 工作室项目中从这个 url 解析 span id = shd2b, https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html
Screenshot in link below
我使用下面的 java 代码来解析它并显示在文本视图框中。
@Override
protected Void doInBackground(Void... voids) {
//Connect to the website
String url = "https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html";
//Get the title of the website
Document doc = null;
doc = (Document) Jsoup.parse(url);
eurotoinr = doc.getElementById("span.shd2b");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
textView.setText((CharSequence) eurotoinr);
}
Bu 当我 运行 时,模拟器设备中没有显示任何内容。有人可以帮我解决这个问题吗?
如果我描述的不够清楚或者看不懂,还请见谅。我只是在这次封锁期间才开始学习 android studio,并且对 stack overflow 很陌生。
我看到两个错误。
- 您是否尝试过调试或显示文档在解析后的外观?它看起来像这样:
<html>
<head></head>
<body>
https://www.exchangerates.org.uk/Euros-to-Rupees-currency-conversion-page.html
</body>
</html>
因为你使用了错误的方法,你的字符串被解析为 HTML 片段。它不会尝试下载任何东西。
你应该使用
doc = Jsoup.connect(url).get();
然后它下载 HTML 并正确解析。
- 您的代码
doc.getElementById("span.shd2b");
将 return 为空,因为span.shd2b
不是有效 ID。 ID 是shd2b;
使用doc.getElementById("shd2b;");
并选择预期的元素:
<span id="shd2b;">82.9337</span>