Android JSOUP 解析网站中的表
我在解析以下网站时遇到问题:
http://www.x -rates.com/d/USD/table.html
我对 Android 编程非常非常陌生,我正在考虑创建一个货币转换器,现在我知道那里有很多,但是,我尝试快速跟踪我的编程入门并让自己沉浸在代码中。
我找到了一个试图解释的教程,它对于需要特定项目的网站很有帮助。代码如下:
package com.ve.webgrab;
import java.io.IOException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class WebgrabActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayList<String> tableRowStrings = new ArrayList<String>();
Document doc = null;
try {
doc = Jsoup.connect("http://www.x-rates.com/d/USD/table.html").get();
}
catch (IOException e) {
e.printStackTrace();
}
String testString = "American";
Elements tableRows = doc.select("tr");
for (Element tableRow : tableRows){
if (tableRow.hasText()){
String rowData = tableRow.text();
if(rowData.contains(testString)){
tableRowStrings.add(rowData);
}
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tableRowStrings);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
我需要能够获取美元到其他货币的表,以便我可以将其保存在数据库中,然后使用它进行转换。我相信我需要查看的部分是:
String testString = "American";
Elements tableRows = doc.select("tr");
for (Element tableRow : tableRows){
if (tableRow.hasText()){
String rowData = tableRow.text();
if(rowData.contains(testString)){
tableRowStrings.add(rowData);
}
}
}
我需要完成什么:
获取网站:- - 我相信这已经完成:-
doc = Jsoup.connect("http://www.x-rates.com/d/USD/table.html").get();
选择网站上对转化率特别感兴趣的表格:- 仍然无法弄清楚这部分。
测试表以确保它是正确的:
String testString = "美国"; if(rowData.contains(testString)){
获取表中的数据并显示到列表视图,以确保收集正确的数据:
ArrayAdapter
;适配器 = new ArrayAdapter (this, android.R.layout.simple_list_item_1, tableRowStrings); setListAdapter(适配器); 将表
分解为国家/地区和转化率,然后将其放入数据库中。
我正在寻求指导,请记住,我对此完全陌生,我想构建这个应用程序来获得学习体验和更大更好的应用程序的跳板,示例代码绝对受欢迎,我将不胜感激我的所有帮助可以得到。
I am having an issue with parsing the following website:
http://www.x-rates.com/d/USD/table.html
I am very, very very new to android programming and I am thinking about creating a currency converter, now I know that there is a bunch out there however, I am trying to fast track my programming initiation and get myself immersed in code.
I found a tutorial that tried to explain and it was helpful for a website with the need for a specific item. the code is below:
package com.ve.webgrab;
import java.io.IOException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class WebgrabActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayList<String> tableRowStrings = new ArrayList<String>();
Document doc = null;
try {
doc = Jsoup.connect("http://www.x-rates.com/d/USD/table.html").get();
}
catch (IOException e) {
e.printStackTrace();
}
String testString = "American";
Elements tableRows = doc.select("tr");
for (Element tableRow : tableRows){
if (tableRow.hasText()){
String rowData = tableRow.text();
if(rowData.contains(testString)){
tableRowStrings.add(rowData);
}
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tableRowStrings);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
I need to be able to get the table with the usd to other currency so that I can have it in a database and then use it for the conversion. I believe the section that I need to look at is:
String testString = "American";
Elements tableRows = doc.select("tr");
for (Element tableRow : tableRows){
if (tableRow.hasText()){
String rowData = tableRow.text();
if(rowData.contains(testString)){
tableRowStrings.add(rowData);
}
}
}
What I need to accomplish:
Get the website:- - I believe this is accomplished:-
doc = Jsoup.connect("http://www.x-rates.com/d/USD/table.html").get();
Select the table on the website with particular interest on the Conversion rate: - Still cannot figure this part out.
Test for the Table to ensure it is the correct one:
String testString = "American"; if(rowData.contains(testString)){
Take the data on the table and display to a list view just to ensure that the correct data is being gathered:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tableRowStrings); setListAdapter(adapter);
Break up the table
<td>
into country and conversion rate and then place it into a database.
I am looking for guidance, please remember that I am totally new to this and I want to build out this app for a learning experience and a spring board to bigger and better apps, example code is definitely welcome, I would appreciate all the help I can get.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我赞成马文的回答,因为从长远来看,使用雅虎提供的网络服务可能会更可靠。
不过,我已经使用另一个网站为您提供了一个基本的解决方案。您可以看到,当网站使用类和 id 来帮助您选择正确的内容时,与迭代所有元素并查找某些内容来找到您想要的信息相比,需要的代码少了多少。您需要稍微修改此代码以检查错误并满足您的需求,但如果您仍然想走这条路,它应该可以帮助您入门。
I've upvoted Marvin's answer because using a webservice that Yahoo provides is going to probably be more reliable in the long run.
However, I have whipped up a basic solution for you using another website. You can see how much less code is needed when the site uses classes and ids to help you select the right things versus iterating through all of the elements and looking for certain things to find the information you want. You'll need to fixup this code a little bit to check for errors and to suit your needs, but it should help you get started if you still want to go down this path.
如果您想要做的只是货币换算,我建议您使用(免费)Yahoo API 来完成此任务。您可以在这个答案中阅读所有详细信息,但本质上您将得到如下内容:
http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv
它将从英镑转换为欧元并轻松生成可解析的文本文件。正如 @B.Anderson 已经指出的那样,在该网站上使用 JSoup 会非常棘手。
If all you're looking to do is currency conversions, I would recommend you use the (free) Yahoo API to get this done. You can read all about the details in this answer, but essentially you'll pull something like the following:
http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv
Which will convert from GBP to EUR and produce an easily parseable text file. As @B.Anderson already pointed out, it's going to be quite tricky using JSoup on that website.