android - 使用 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
解决方案
不知道是不是最好的方法,但它有效
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)
}
}
}
}
推荐阅读
- reactjs - React-Bootstrap v1 中的 Panel.Collapse
- android - 禁用 FRP 的用户帐户,仅允许工作管理帐户
- python - Python 3:删除文件中最后一个字符的惯用方法
- javascript - WebStorm diff 中一行上的黄色块是什么意思?
- ios - ios上的cordova cli命令失败:ENOENT cordova-config.json.SOME_NUMBER
- python - select appropriate dataframe based on user input
- c# - 无法将 lambda 表达式转换为类型“DbContextOptions
" 因为它不是委托类型 - javascript - 创建一个前端的两个请求?
- c - 这里使用信号的方式背后的想法是什么?
- java - AdapterView 不支持 addView(View, LayoutParams) - Android 中的自定义 ListView 崩溃