簡體   English   中英

使用jsoup解析網站數據

[英]Parse data from website using jsoup

我正在創建一個Android應用,該應用從www.zamboangatoday.ph收集數據,獲取所有新聞標題或標題。 但我只能檢索一項,有人可以檢查我的代碼。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



            try{

                outputTextView = (TextView)findViewById(R.id.textView1);
                //Document doc = Jsoup.parse(html,"http://www.zamboangatoday.ph");
                Document doc = Jsoup.connect("http://www.zamboangatoday.ph/").get();
                //Elements tag = doc.select(".even h4 a");

                Iterator<Element> iter = doc.select("li.even h4 a").iterator();
                //List<Image> images = new ArrayList<Image>();
                while(iter.hasNext())
                {
                    Element element = iter.next();

                    outputTextView.setText(element.text());

                }

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




    }

不知道什么組件outputTextView是,但是使用以下代碼:

outputTextView.setText(element.text());

您將設置文本,並在每次迭代時將其覆蓋 因此,在循環之后,只有最后一個元素文本。 如果可能,請使用諸如outputTextView.append(element.text());類的東西outputTextView.append(element.text()); -否則使用StringBuilder並在循環后設置文本。


outputTextView = (TextView)findViewById(R.id.textView1);
//Document doc = Jsoup.parse(html,"http://www.zamboangatoday.ph");
Document doc = Jsoup.connect("http://www.zamboangatoday.ph/").get();
//Elements tag = doc.select(".even h4 a");

Iterator<Element> iter = doc.select("li.even h4 a").iterator();
//List<Image> images = new ArrayList<Image>();
while(iter.hasNext())
{
    Element element = iter.next();

    /*
     * Append the text - don't overwrite it
     * Node: Maybe you have to add a new line ('\n').
     */
    outputTextView.append(element.text()); 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM