首页 > 解决方案 > 使用 JSOUP 从 HTML 解析 JSON 中获取图像 URL

问题描述

我第一次使用 Jsoup 并尝试获取图像 URL,但似乎无法获取。

这是我到达的地方,但现在我有一个 JSON 格式的节点。是否可以使用 JSOUP 仅获取 URL?

fun shouldParseHTML(url : String) {
 var document = Jsoup.connect("https://boardgamegeek.com/boardgame/256788").get()
     var scripts = document.body().select("script")
        for (element : Element in scripts){
            println("element" + element.childNode(0))
        }
}

这就是它返回的内容

{
    "@context": "http://schema.org",
    "@type": "Product",
    "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "7.37661",
        "reviewCount": "242",
        "bestRating": "10",
        "worstRating": "1"
    },
    "image": "https://cf.geekdo-images.com/itemrep/img/TqDQErcijlN-gz8an0d7sm5AXUU=/fit-in/246x300/pic4783811.jpg",
    "name": "Detective Club"
}

这是它在里面的元素:

<script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Product",
        "aggregateRating": {
            "@type": "AggregateRating",
            "ratingValue": "7.37661",
            "reviewCount": "242",
            "bestRating": "10",
            "worstRating": "1"
        },
        "image": "https://cf.geekdo-images.com/itemrep/img/TqDQErcijlN-gz8an0d7sm5AXUU=/fit-in/246x300/pic4783811.jpg",
        "name": "Detective Club"
    }
    </script>

感谢:D

标签: androidhtmljsonkotlinjsoup

解决方案


不知道是不是最好的方法,但它有效

if (gameModel.image == null || gameModel.image.isEmpty()){
            var document = Jsoup.connect("https://boardgamegeek.com/boardgame/${gameModel.bggId}").get()
            var scripts = document.body().select("script")
            for (element : Element in scripts){
                if (element.childNodeSize() == 1){
                    if (element.childNode(0).toString().contains("image", true)){
                        gameModel.image =  element.childNode(0).toString().subSequence(element.childNode(0).toString().indexOf("image")+9,element.childNode(0).toString().indexOf("name")-8).toString()
                        position = listOfGamesModel.gamesList.indexOf(gameModel)
                    }
                }
            }
        }

推荐阅读