如何使用 jsoup 解析特定的 table 数据?
How can I parse specific table data using jsoup?
我正在开发一个小项目,我试图在这个 link 上解析来自 table 的农作物的更新市场价格:
http://amis.pk/ViewPrices.aspx?searchType=1&commodityId=1
我想得到这样的输出,Apple(ammre):12500
我使用的代码是:
public class MainActivity extends AppCompatActivity {
private String url="http://amis.pk/ViewPrices.aspx?searchType=1&commodityId=1";
TextView datatv;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datatv=(TextView)findViewById(R.id.tv);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.CUPCAKE)
@Override
public void onClick(View view) {
new Description().execute();
}
});
}
@TargetApi(Build.VERSION_CODES.CUPCAKE)
private class Description extends AsyncTask<Void, Void, Void> {
StringBuilder s=new StringBuilder();
String title;
@Override
protected Void doInBackground(Void... params) {
try {
Document mBlogDocument = Jsoup.connect(url).get();
Log.e("Activity Log", "doInBackground"+mBlogDocument.toString());
Elements table = mBlogDocument.getElementsByClass("table.cart");
Elements tdsInSecondRow = mBlogDocument.select("table tr:nth-child(2) > td");
for (Element td : tdsInSecondRow)
{
System.out.println("TD: " + td.text());
}
s.append(table);
s.append(tdsInSecondRow);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
此代码向我返回了第二行中 table 的完整 html 数据,但我如何才能仅从特定于 apple(ammre) 的第 4 列(最高价格)中获取数据?我对此一无所知。任何帮助将不胜感激。
此代码获取所有 table 行并逐行打印:
Document document = Jsoup.connect(url).get();
Elements rows = document.select("#amis_prices").select("tr:not(.labelLists)");
for (Element row : rows) {
String name = row.select(".listItem").text();
String maxPrice = row.select(".pricedata:nth-of-type(3)").text();
System.out.println(name + ": " + maxPrice); // or what is appropriate in your code
}
请注意,如果您正在为 android 编码,请将最后一行 System.out...
替换为适合您的代码的内容 - 例如button.setText(name + maxPrice)
或...
如果您只想获得第二行,可以这样做:
Document document = Jsoup.connect(url).get();
Elements row = document.select("#amis_prices").select("tr:nth-of-type(2)"); // this 2 means the second row that you wanted
String name = row.select(".listItem").text();
String maxPrice = row.select(".pricedata:nth-of-type(3)").text();
System.out.println(name + ": " + maxPrice); // or what is appropriate in your code
我正在开发一个小项目,我试图在这个 link 上解析来自 table 的农作物的更新市场价格:
http://amis.pk/ViewPrices.aspx?searchType=1&commodityId=1
我想得到这样的输出,Apple(ammre):12500
我使用的代码是:
public class MainActivity extends AppCompatActivity {
private String url="http://amis.pk/ViewPrices.aspx?searchType=1&commodityId=1";
TextView datatv;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datatv=(TextView)findViewById(R.id.tv);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.CUPCAKE)
@Override
public void onClick(View view) {
new Description().execute();
}
});
}
@TargetApi(Build.VERSION_CODES.CUPCAKE)
private class Description extends AsyncTask<Void, Void, Void> {
StringBuilder s=new StringBuilder();
String title;
@Override
protected Void doInBackground(Void... params) {
try {
Document mBlogDocument = Jsoup.connect(url).get();
Log.e("Activity Log", "doInBackground"+mBlogDocument.toString());
Elements table = mBlogDocument.getElementsByClass("table.cart");
Elements tdsInSecondRow = mBlogDocument.select("table tr:nth-child(2) > td");
for (Element td : tdsInSecondRow)
{
System.out.println("TD: " + td.text());
}
s.append(table);
s.append(tdsInSecondRow);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
此代码向我返回了第二行中 table 的完整 html 数据,但我如何才能仅从特定于 apple(ammre) 的第 4 列(最高价格)中获取数据?我对此一无所知。任何帮助将不胜感激。
此代码获取所有 table 行并逐行打印:
Document document = Jsoup.connect(url).get();
Elements rows = document.select("#amis_prices").select("tr:not(.labelLists)");
for (Element row : rows) {
String name = row.select(".listItem").text();
String maxPrice = row.select(".pricedata:nth-of-type(3)").text();
System.out.println(name + ": " + maxPrice); // or what is appropriate in your code
}
请注意,如果您正在为 android 编码,请将最后一行 System.out...
替换为适合您的代码的内容 - 例如button.setText(name + maxPrice)
或...
如果您只想获得第二行,可以这样做:
Document document = Jsoup.connect(url).get();
Elements row = document.select("#amis_prices").select("tr:nth-of-type(2)"); // this 2 means the second row that you wanted
String name = row.select(".listItem").text();
String maxPrice = row.select(".pricedata:nth-of-type(3)").text();
System.out.println(name + ": " + maxPrice); // or what is appropriate in your code