【问题标题】:How can I parse specific table data using jsoup?如何使用 jsoup 解析特定的表数据?
【发布时间】:2018-07-04 13:53:23
【问题描述】:

我正在开发一个小项目,我试图从这个链接的表格中解析农作物的最新市场价格:
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)的数据?我对此一无所知。任何帮助将不胜感激。

【问题讨论】:

  • 如果没有包含足够的页面 HTML 来说明您的代码在做什么,这个问题就离题了。 Stack Overflow 上的问题必须是自包含的。这意味着我们需要能够回答问题,而无需离开现场即可获取特定问题的信息(例如,必须访问网站)。
  • 感谢您添加此信息,我会牢记这一点。不过之前的回复帮了大忙。

标签: java parsing web-scraping jsoup


【解决方案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

【讨论】:

  • 所有表格标题都在类“.listItem”中。这应该返回完整列表吗?或者这不起作用。单击按钮不返回任何数据。
  • 我的代码返回所有行,如Apple (Ammre): 12500 然后Apple (Gatcha): - 等等。你只需要第二排吗?请注意,不要在最后一行中使用System.out.println,而是在您的 android 代码中执行适当的操作。
  • 我需要至少十个,但单独一个。就像单击苹果按钮一样,它在另一个按钮上返回“Apple(Ammre):12500”,它返回下一个按钮,依此类推。但目前我正在尝试使用第一个。但是您的代码仍然没有运行。我正在使用它。
  • 我可以完全理解这段代码。谢谢你。我在android中使用log.d,但它仍然没有打印。我正在尝试可能是我在某个地方错了。
猜你喜欢
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 2020-06-01
  • 2012-12-27
  • 1970-01-01
  • 2020-10-24
  • 2018-11-19
  • 1970-01-01
相关资源
最近更新 更多