如何使用jsoup解析lu,li标签? [英] How parse lu, li tags with jsoup?
问题描述
我知道,对此有很多问题,但是没有答案帮助了我.
试图从一个著名的乌克兰门户网站解析足球新闻,并放在我的列表视图中.
I know, there are a lot of questions on this, but no answer helped me.
Trying to parse football news from one famous ukrainian portal and put to my listview.
我解析了新闻提要"类:
I parsed "news-feed" class:
class ParseTitle extends AsyncTask<Void, Void, HashMap<String, String>>{
@Override
protected HashMap<String, String> doInBackground(Void... params) {
HashMap<String, String> hashMap = new HashMap<>();
try {
Document document = Jsoup.connect("http://football.ua/england.html").get();
Elements elements = document.select(".news-feed");
for (Element element : elements){
Element element1 = element.select("a[href]").first();
hashMap.put(element.text(), element1.attr("abs:ahref"));
}
} catch (IOException e) {
e.printStackTrace();
}
return hashMap;
}
}
推荐答案
使用
Elements elements = document.select("article.news-feed");
代替
Elements elements = document.select(".news-feed");
将我的代码与您的代码进行比较,我看到了很大的不同,首先,我认为更重要的是,您将读取值存储在HashMap中,而我则存储在StringBuffer中.然后,我连接并按照这种方式进行:
comparing my code to yours, I see good differences, firstly and I think more important, you accumulate the read values in a HashMap, I in a StringBuffer. Then I connect and go this way:
try {
doc = Jsoup.connect("http://football.ua/england.html").userAgent("yourPersonalizedUA").timeout(0).ignoreHttpErrors(true).get();
topicList = doc.select("article.news-feed");
for (Element topic : topicList) {
myString += topic.html();
}} catch (IOException e) { System.out.println("io - "+e); }
buffer.append(myString);
然后,如果一切正常
return buffer.toString();
假设您已经在开始时说过:
Presuming you've already stated at the beggining:
private Document doc;
private String myString;
private StringBuffer buffer;
private Elements topicList;
不确定是否有帮助,也许可以带来新的视角.您是否成功使用代码解析了另一个页面?
Not shure if this helps, maybe can lead into a new perspective. Have you succeeded parsing another page with your code?
这篇关于如何使用jsoup解析lu,li标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!