2019-10-16

//使用jsoup


  new Thread(new Runnable() {
            @Override
            public void run() {
                Document doc = null;
                try {
                    final ArrayList list = new ArrayList<>();
                    doc = Jsoup.connect("https://www.v2ex.com/").get();

                    Element select = doc.select("div#Tabs").last();
                    Elements tabs = select.select("a");
                    for (Element element : tabs) {
                        String text = element.text();
                        String href = element.attr("href");
                        TabBean tabBean = new TabBean(text, href);
                        list.add(tabBean);
                        Log.i(TAG, "run: " + text + "====" + href);
                    }
                    //子条目:需要查找所有class 是cell.item的div标签/元素
                    Elements items = doc.select("div.cell.item");
                    for (Element item : items) {
                        //头像
                        Element image = item.select("table tbody tr td a img.avatar").first();
                        String src = image.attr("src");
                        //Logger.println("头像:"+src);

                        //评论数量
                        Elements comments = item.select("table tbody tr td a.count_livid");
                        if (comments != null && comments.size() > 0) {
                            String commentCount = comments.first().text();
                            //Logger.println("评论数量:"+commentCount);
                        }

                        //标题
                        Element title = item.select("table tbody tr td span.item_title a" +
                                ".topic-link").first();
                        String text = title.text();
                        //标题下边的部分
                        Element first = item.select("table tbody tr td span.topic_info").first();
                        String topic = first.text();
                    }
                    Message message = Message.obtain();
                    message.obj = list;
                    handler.sendMessage(message); //在新的线程中进行发送Message消息
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "run: " + e.getMessage());
                }
            }

        }).start();

你可能感兴趣的:(2019-10-16)