简体   繁体   English

使用Jsoup Android解析HTML时URL错误

[英]Wrong URL when parsing HTML with Jsoup Android

could you help me with parsing html site? 您能帮我解析HTML网站吗? I need get src of image and link to another page, but I don't know why I get empty list This is my code: 我需要获取图片的src并链接到另一页,但是我不知道为什么我得到空列表这是我的代码:

Elements elems2 = doc.select("div");
for (Element elem2 : elems2) {
    if (elem2.attr("class").equals("grid-box-img")) {
        System.out.println(elem2.attr("img"));
        kfunewphoto.add(elem2.attr("src"));
    }
}

and example of html: 和html的示例:

<div class="grid-box-img"><a href="http://cleverrussia.com/shou-talanty-uspej-uvidet-pervym/" rel="bookmark" title="Шоу &#8220;Таланты&#8221;. Успей увидеть первым!"><img width="680" height="470" src="https://i.stack.imgur.com/c7PGK.png" class="attachment-full wp-post-image" alt="shou-talanty-uspej-uvidet-pervym-clever-russia" /></a></div>

I need get " http://cleverrussia.com/wp-content/uploads/2014/10/shou-talanty-uspej-uvidet-pervym-clever-russia.png " and the second part of code: 我需要获取“ http://cleverrussia.com/wp-content/uploads/2014/10/shou-talanty-uspej-uvidet-pervym-clever-russia.png ”和第二部分代码:

            Elements elems = doc.select("h2");
            for (Element elem : elems) {
                if (elem.attr("class").equals("entry-title")) {
                    str = elem.text();
                    kfunews.add(elem.text());
                    kfunewslist1.add(elem.attr("href"));
                }

<h2 class="entry-title"><a href="http://cleverrussia.com/shou-talanty-uspej-uvidet-pervym/" title="Permalink to Шоу &#8220;Таланты&#8221;. Успей увидеть первым!" rel="bookmark">Шоу &#8220;Таланты&#8221;. Успей увидеть первым!</a></h2>

And I need get: " http://cleverrussia.com/shou-talanty-uspej-uvidet-pervym/ " 我需要得到:“ http://cleverrussia.com/shou-talanty-uspej-uvidet-pervym/
This is full code of page - view-source: http://cleverrussia.com/ 这是页面的完整代码-查看源代码: http : //cleverrussia.com/

The error is that you're trying to select img and a as attributes. 错误是您正在尝试选择img和a作为属性。 Check the below code to see how to fix your code. 检查下面的代码,看看如何修复您的代码。

// Prints the image source
System.out.println(elem2.select("img").attr("src"));
kfunewphoto.add(elem2.select("img").attr("src"));
// Prints the target link
System.out.println(elem.select("a").attr("href"));
kfunewslist1.add(elem.select("a").attr("href"));

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

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