使用Jsoup从网站获取数据? [英] getting data from website using Jsoup?
问题描述
尝试使用Jsoup(而不是网站API)从网站练习并获取信息. 我的代码没有错误,但文本字段未更改.它只是给我显示一个空白.我如何从网站上获取信息?我正在尝试获取主要新闻,以便可以在我的网站上发布.
Trying to practice and get info from website using Jsoup not the website 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.
我的代码:
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);
}
}
}
推荐答案
问题出在这里:
@Override
protected void onPostExecute(Void feed) {
String ankosh = document.attr("href");
text.setText(ankosh);
}
document
变量没有名为href
的属性.这就是ankosh
变量为空的原因.
The document
variable doesn't have an attribute called href
. This is why the ankosh
variable is empty.
相反,请尝试以下操作:(如果文档中具有fc-item__content类的第一个div,我想是主要新闻).
Instead try this: (I suppose the main news if the first div with fc-item__content class in the document).
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());
}
最后一点,您应该避免使用Jsoup.connect
加载文档.在内部,它使用了URL
类,该类非常慢.请改用Volley.请查看此示例代码,其中显示了对Volley和Jsoup的用法.
One last note, you should avoid Jsoup.connect
for loading the document. Internally, it uses the URL
class which is notoriously slow. Use Volley instead. Please see this sample code showing the use of Volley and Jsoup.
这篇关于使用Jsoup从网站获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!