Golang提取JSON-ld数据的实用方法
Golang提取JSON-ld数据的实用方法 我的目标是从网页中提取结构化数据。
我能够使用 Go 语言中的 microdata 库从包含微数据的网页中提取 HTML5 微数据(Schema.org)。但是,我无法提取网页中存在的 JSON-LD 格式。没有找到关于这方面的进一步文档。
你好 lutzhorn,
感谢分享。我会尝试这个方法,如果有其他解决方案请告诉我。
更多关于Golang提取JSON-ld数据的实用方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
你能提供一个包含JSON-LD的示例网页链接吗?
你使用哪个Go库来提取HTML5微数据?这个库能够提取JSON-LD吗?
// 代码示例保留原样
你好 lutzhorn
JSON-LD 数据链接:

2018 国际评审展览
评审讲座:11月10日,在卡彭特大厅,下午4点至5点,随后在画廊举行招待会,下午5点至7点。摄影艺术中心很高兴宣布2018年国际评审展览!今年我们提供6000美元…
我找不到任何 JSON-LD 的库。在 Golang 中用于微数据的库是 IAND 微数据。
从 URL 获取 JSON-LD 输出:
"json-ld": [
{
"@context": "http://schema.org",
"@type": "Event",
"description": "<p>Juror's Lecture: November 10, in Carpenter Hall, from 4 to 5 PM, followed by a reception in the gallery from 5 to 7 […]</p>\n",
"endDate": "2018-12-23T23:59:59-08:00",
"image": "https://d11pj51h4ledry.cloudfront.net/wp-content/uploads/2018/07/20182348/2018-IJE-LOGO3-1-e1532136291438.jpg",
"location": {
"@type": "Place",
"address": {
"@type": "PostalAddress",
"addressCountry": "United States",
"addressLocality": "Carmel",
"addressRegion": "CA",
"postalCode": "93921",
"streetAddress": "San Carlos and 9th"
},
"description": "",
"geo": {
"@type": "GeoCoordinates",
"latitude": 36.5513147,
"longitude": -121.9218091
},
"name": "Center for Photographic Art",
"sameAs":
"telephone": "831 625-5181",
"url":
},
"name": "2018 International Juried Exhibition <br>",
"organizer": {
"@type": "Person",
"description": "",
"email":
"name": "CPA",
"sameAs":
"telephone": "831 625-5181",
"url":
},
"startDate": "2018-11-10T00:00:00-08:00",
"url":
}
]
在Go语言中提取JSON-LD数据,可以使用标准库encoding/json结合HTML解析库(如goquery)来实现。以下是详细步骤和示例代码:
步骤:
- 使用HTTP客户端获取网页内容。
- 解析HTML文档,定位JSON-LD脚本标签(
<script type="application/ld+json">)。 - 提取JSON数据并解析为Go结构体或映射。
示例代码:
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
// 定义通用结构体来解析JSON-LD(根据Schema.org调整字段)
type JSONLD struct {
Context string `json:"@context"`
Type string `json:"@type"`
Name string `json:"name"`
URL string `json:"url"`
// 添加其他所需字段
}
func main() {
// 示例URL(替换为目标网页)
url := "https://example.com"
resp, err := http.Get(url)
if err != nil {
log.Fatal("HTTP请求失败:", err)
}
defer resp.Body.Close()
// 解析HTML文档
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal("HTML解析失败:", err)
}
// 查找所有JSON-LD脚本标签
doc.Find("script[type='application/ld+json']").Each(func(i int, s *goquery.Selection) {
jsonText := strings.TrimSpace(s.Text())
if jsonText == "" {
return
}
// 解析JSON数据
var data JSONLD
if err := json.Unmarshal([]byte(jsonText), &data); err != nil {
log.Printf("JSON解析失败(脚本索引 %d): %v\n", i, err)
return
}
// 输出提取的数据
fmt.Printf("JSON-LD 数据 [%d]:\n", i)
fmt.Printf(" 类型: %s\n", data.Type)
fmt.Printf(" 名称: %s\n", data.Name)
fmt.Printf(" URL: %s\n", data.URL)
// 打印其他字段
})
}
说明:
- 依赖库:使用
goquery解析HTML(通过go get github.com/PuerkitoBio/goquery安装)。 - 灵活性:
JSONLD结构体可根据实际JSON-LD模式扩展字段(例如Article、Organization等)。 - 错误处理:代码包含网络请求、解析和JSON解码的错误检查。
处理多个JSON-LD对象:
如果网页包含多个JSON-LD脚本或嵌套数组,可使用[]JSONLD或interface{}解析:
var data []JSONLD
// 或
var data interface{}
此方法适用于大多数包含JSON-LD的网页,如使用Schema.org标记的产品、文章或组织信息。

