简体   繁体   English

使用Jsoup从网站获取数据?

[英]getting data from website using Jsoup?

Trying to practice and get info from website using Jsoup not the website API. 尝试使用Jsoup而非网站API从网站练习并获取信息。 My code does not have an error but the text field is not changing. 我的代码没有错误,但文本字段未更改。 It just shows me a blank space. 它只是显示一个空白。 How would i get info from the website? 我如何从网站上获取信息? i'm trying to obtain the Main News so i could post on my website. 我正在尝试获取主要新闻,以便可以在我的网站上发布。

My Code: 我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.io.InputStream;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Document document;
    TextView text;
    String ankosh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView)findViewById(R.id.textView);
        new Ankosh().execute();

    }

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

        private Exception exception;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                // Connect to the web site
                document = Jsoup.connect("http://www.theguardian.com/us").get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void feed) {

            String ankosh = document.attr("href");
            text.setText(ankosh);



        }
    }


}

The problem is lying here: 问题就在这里:

@Override
protected void onPostExecute(Void feed) {
    String ankosh = document.attr("href");
    text.setText(ankosh);
}

The document variable doesn't have an attribute called href . document变量没有名为href的属性。 This is why the ankosh variable is empty. 这就是ankosh变量为空的原因。

Instead try this: ( I suppose the main news if the first div with fc-item__content class in the document ). 而是尝试以下操作:( 我想主要新闻是,如果第一个div在文档中具有fc-item__content类 )。

Element mainNewsDiv = document.select("div.fc-container--rolled-up-hide.fc-container__body > div:nth-child(1) > ul > li > div > div > div.fc-item__content").first();

if (mainNewsDiv == null) {
    // Main news not found...
} else {
    text.setText(mainNewsDiv.text());
}

One last note, you should avoid Jsoup.connect for loading the document. 最后一点,您应避免使用Jsoup.connect加载文档。 Internally, it uses the URL class which is notoriously slow. 在内部,它使用非常慢的URL类。 Use Volley instead. 请改用Volley。 Please see this sample code showing the use of Volley and Jsoup . 请参阅此示例代码,其中显示了Volley和Jsoup的用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM