java - 怎么获取到text中的文本,或者title中的值
ringa_lee
ringa_lee 2017-04-17 17:59:03
0
6
251

用Document解析html,我现在要怎样获取到 ”防触电插座保护盖“ 这个数据

<p class="fb_fx">
                <p id="bdshare" class="bdshare_t bds_tools get-codes-bdshare" style="display:none" data="{'comment':'','desc':'防触电插座保护盖 ','text':'防触电插座保护盖 ','title':'防触电插座保护盖 ','url':'http://www.tsdxb.comhttp://item.taobao.com/item.htm?id=521916721437','pic':'http://www.tsdxb.com/otherimg/imgju/2016/0103/7-521916721437.jpg'}">
                <a class="bds_tsina" title="分享到新浪微博" href="#"></a>
                <a href="#" class="bds_qzone qqkj" title="分享到QQ空间"></a>
                <a class="bds_tqq" title="分享到腾讯微博" href="#"></a>
                <a href="#" class="bds_mshare mshare" title="一键分享"></a>
                </p>
            </p>
ringa_lee
ringa_lee

ringa_lee

reply all(6)
左手右手慢动作

Try this?

public static void main(String[] args) {
    Document doc = null;//这里是你自己获取的Document对象

    Element share = doc.getElementById("bdshare");
    String data = share.getAttribute("data");

    Gson gson = new Gson();
    Data dd = gson.fromJson(data, Data.class);
    System.out.println(dd.getText());
}

class Data {
    private String comment;
    private String desc;
    private String text;
    private String title;
    private String url;
    private String pic;

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
}
迷茫
eval("var a="+window.document.getElementById('bdshare').getAttribute('data'))
console.log(a.title)
洪涛
var el = document.getElementById('bdshare')
var data = JSON.parse(el.getAttribute('data'))
//title
alert(data.title)
//text
alert(data.text)
伊谢尔伦

I quite agree with the method of using json parse.

巴扎黑

There are three ways to achieve this:
var data = window.document.getElementById('bdshare').getAttribute('data');

  1. var json = JSON.parse(data);

  2. eval('(' + data + ')');

  3. var a = new Function("return " + data); a();

小葫芦

documentby or use jquery

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!