Java Jsoup Android解析
我正在解析一个网页。。。现在当我做完了。。。我知道我在做一些非常愚蠢的事情,所以如果有人能向我指出这一点:)那么正确的方向是什么 我有一个使用Jsoup的Android应用程序,它工作得很好,但速度非常慢!我知道为什么。。。因为基本上在创建时,我有20,30个Jsoup getElement请求Java Jsoup Android解析,java,android,jsoup,Java,Android,Jsoup,我正在解析一个网页。。。现在当我做完了。。。我知道我在做一些非常愚蠢的事情,所以如果有人能向我指出这一点:)那么正确的方向是什么 我有一个使用Jsoup的Android应用程序,它工作得很好,但速度非常慢!我知道为什么。。。因为基本上在创建时,我有20,30个Jsoup getElement请求 private class Task extends AsyncTask<Void, Void, Void>{ String linkText; @Override
private class Task extends AsyncTask<Void, Void, Void>{
String linkText;
@Override
protected Void doInBackground(Void... params) {
Initdata();
return null;
}
@Override
protected void onPostExecute(Void param) {
mProgressBarHandler.hide();
redraw();
inflatedView.invalidate();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBarHandler.show();
}
}
知道如何加快速度吗?在每次调用GetElement时,您都要求重复解析相同的文档!当然慢了
相反,只需调用一次JSoup就可以获取文档,然后使用它返回的document对象对该文档进行所有查询。对于初学者,请发布实际使用JSoup的代码。此外,您还需要提供一些它所使用的示例HTML文档。在这里,您只是展示了一个基本的异步任务。@DougStevenson请检查更新的问题。该网页是www.istinomer.rs您的意思是类似于公共文档GetElement(String元素,String站点,int模式){Elements newshheadlines=null;if(mode==1){Document doc=Jsoup.connect(site).timeout(600000).get();return doc;}每次执行
Document doc=Jsoup.connect(site).timeout(600000).get()时,我都会对这个函数返回的文档对象执行查询
它每次都会抓取页面。没有缓存。你应该只做一次,然后为该站点上的所有查询存储文档。我想我明白了。我现在尝试一个快速测试。是的,我知道我在做一些愚蠢的事情,只是不知道是什么。现在大约快了4倍。我能做更多的事情来优化速度吗?
private void Initdata(){
loadImages();
players = new String[] {util.GetElement("div.item-2:first-child", "http://www.istinomer.rs/", 0),
util.GetElement("div.item-2:nth-child(2)", "http://www.istinomer.rs/", 0),
util.GetElement("div.item-2:nth-child(3)", "http://www.istinomer.rs/", 0),
util.GetElement("div.item-2:nth-child(4)","http://www.istinomer.rs/",0),
util.GetElement("div.item-2:nth-child(5)","http://www.istinomer.rs/",0),
util.GetElement("div.item-2:nth-child(6)","http://www.istinomer.rs/",0),
util.GetElement("div.item-2:nth-child(7)","http://www.istinomer.rs/",0),
util.GetElement("div.item-2:nth-child(8)","http://www.istinomer.rs/",0),
util.GetElement("div.item-2:nth-child(9)","http://www.istinomer.rs/",0),
util.GetElement("div.item-2:nth-child(10)","http://www.istinomer.rs/",0)
};
vestiDescription1 = util.GetElement("div.item-big h2", "http://www.istinomer.rs/", 0) + System.getProperty("line.separator")
+ util.GetElement("div.item-big h3","http://www.istinomer.rs/",0);
vestiDescription2 = util.GetElement("div.grid-8 h2 a", "http://www.istinomer.rs/", 0) + System.getProperty("line.separator")
+ util.GetElement2("div.grid-8 h3","http://www.istinomer.rs/",0);
vestiDescription3 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(4) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
vestiDescription4 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(5) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
vestiDescription5 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
vestiDescription6 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(2) > h2:nth-child(4)", "http://www.istinomer.rs/", 0) + System.getProperty("line.separator")
+ util.GetElement2("div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(2) > h3:nth-child(5)","http://www.istinomer.rs/",0);
vestiDescription7 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
vestiDescription8 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(4) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
vestiDescription9 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(5) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
vestiDescription10 = util.GetElement(
"div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
currency = new String[]{
vestiDescription1,
vestiDescription2,
vestiDescription3,
vestiDescription4,
vestiDescription5,
vestiDescription6,
vestiDescription7,
vestiDescription8,
vestiDescription9,
vestiDescription10
};
public String GetElement(String Element, String site, int mode) {
try {
Elements newsHeadlines = null;
if (mode == 0) {
Document doc = Jsoup.connect(site).timeout(600000).get();
newsHeadlines = doc.select(Element);
}
//1 gets link from class
else if (mode == 1) {
Document doc = Jsoup.connect(site).timeout(600000).get();
String link = doc.select(Element).toString();
return link;
}
//Log.d("TMS", "Data is " + html2text(newsHeadlines.toString()));
String returnData = html2text(newsHeadlines.toString());
return returnData;
}
catch (Exception e) {
Log.d("TMS", "EXCEPTION GetElement: " + Element);
e.printStackTrace();
return "Error";
}