Jsoup,选择元素不起作用

时间:2017-04-14 10:00:05

标签: java android android-studio jsoup

Jsoup选择元素选择div不是,但是孔jsoup正在工作而不是选择元素。

TextView text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView)findViewById(R.id.tb);
    Button btn = (Button)findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new doit().execute();
        }
    });

}

public class doit extends AsyncTask<Void,Void,Void> {

    String words;

    @Override
    protected Void doInBackground(Void... params) {

        try {

            Document doc = Jsoup.connect("http://www.blablabla.com/").get();
            Element newsHeadlines = doc.body().getElementById("div#content");
            System.out.println(newsHeadlines);
            words = doc.text();

        }catch (Exception e){e.printStackTrace();}

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        text.setText(words);

    }
}

2 个答案:

答案 0 :(得分:0)

尝试

Element newsHeadlines = doc.select("div#content");

答案 1 :(得分:0)

将您的来源更改为此

public class doit extends AsyncTask<Void,Void,String> {

@Override
protected String doInBackground(Void... params) {
    String words = "";
    try {

        Document doc = Jsoup.connect("http://www.blablabla.com/").get();
        Element newsHeadlines = doc.body().getElementById("div#content");
        System.out.println(newsHeadlines);
        words = doc.text();

    }catch (Exception e){e.printStackTrace();}

    return words;
}

@Override
protected void onPostExecute(String words) {
    super.onPostExecute(aVoid);
    text.setText(words);

}

}