【问题标题】:Get Image URL from HTML parse JSON using JSOUP使用 JSOUP 从 HTML 解析 JSON 中获取图像 URL
【发布时间】:2019-12-23 11:34:39
【问题描述】:

我第一次使用 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

【问题讨论】:

    标签: android html json kotlin jsoup


    【解决方案1】:

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

    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)
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2012-11-06
      相关资源
      最近更新 更多