如何用jsoup 解析lu, li 标签?
How parse lu, li tags with jsoup?
我知道,有很多关于这个的问题,但没有一个答案对我有帮助。
试图从一个著名的乌克兰门户网站解析足球新闻并将其放入我的列表视图中。
我解析了 "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 中。然后我连接并走这条路:
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();
假设您已经在开头声明:
private Document doc;
private String myString;
private StringBuffer buffer;
private Elements topicList;
不确定这是否有帮助,也许可以带来新的视角。您是否成功使用您的代码解析了另一个页面?
我知道,有很多关于这个的问题,但没有一个答案对我有帮助。
试图从一个著名的乌克兰门户网站解析足球新闻并将其放入我的列表视图中。
我解析了 "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 中。然后我连接并走这条路:
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();
假设您已经在开头声明:
private Document doc;
private String myString;
private StringBuffer buffer;
private Elements topicList;
不确定这是否有帮助,也许可以带来新的视角。您是否成功使用您的代码解析了另一个页面?