我正在开发一个小项目,在这个项目中,我试图从此链接的表中解析农作物的最新市场价格:
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,但是我如何仅从apple(ammre)的第4列(最高价格)获取数据?我对此一无所知。任何帮助将不胜感激。
答案 0 :(得分:-1)
此代码获取所有表行并一一打印:
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