java - 如何使用 jsoup 解析特定的表数据?
问题描述
我正在开发一个小项目,我试图从这个链接的表格中解析农作物的最新市场价格:
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;
}
这段代码在第二行向我返回了完整的 html 表数据,但是我怎样才能只从第 4 列(最高价格)中获取特定于苹果(ammre)的数据?我对此一无所知。任何帮助将不胜感激。
解决方案
此代码获取所有表行并一一打印:
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