带有 Nokogiri 的脚本标记中的目标元素
Target element in script tag with Nokogiri
任何人都可以帮助我使用 Nokogiri 从这个脚本标签中提取作者姓名。
到目前为止,我可以使用以下方法访问脚本标签:
parsed_page.xpath("//脚本[@type ='application/ld+json']")
我正在尝试获取“Kevin McCart”这个名字
<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"NewsArticle","headline":"OLYMPICS: Check out schedule","url":"https:\/\/www.website.ie\/sport\/winter-olympics-check-out-jack-gowers-schedule-4237649","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/www.southernstar.ie\/sport\/winter-olympics-check-out-jack-gowers-schedule-4237649"},"dateCreated":"2022-02-04T12:00:40+00:00","datePublished":"2022-02-04T12:00:40+00:00","dateModified":"2022-02-02T15:08:29+00:00","thumbnailUrl":"https:\/\/images.website.ie\/uploads\/2022\/01\/24153939\/Jack-Gower-cropped.jpg","image":{"@type":"ImageObject","url":"https:\/\/images.website.ie\/uploads\/2022\/01\/24153939\/Jack-Gower-cropped.jpg","width":700,"height":370},"articleSection":"Sport","keywords":"Jack Go","author":[{"@type":"Person","name":"Kevin McCart"}],"publisher":{"@type":"Organization","name":"The Southern Star","logo":{"@type":"ImageObject"}}}</script>
任何帮助将不胜感激。
谢谢,
杰拉德
一旦你到达脚本标签,nokogiri 的工作就完成了,是时候解析了 JSON。
require 'json'
def get_kevin(script_element)
data = JSON.parse(script_element.text)
data.fetch("author").first.fetch("name")
end
任何人都可以帮助我使用 Nokogiri 从这个脚本标签中提取作者姓名。
到目前为止,我可以使用以下方法访问脚本标签:
parsed_page.xpath("//脚本[@type ='application/ld+json']")
我正在尝试获取“Kevin McCart”这个名字
<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"NewsArticle","headline":"OLYMPICS: Check out schedule","url":"https:\/\/www.website.ie\/sport\/winter-olympics-check-out-jack-gowers-schedule-4237649","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/www.southernstar.ie\/sport\/winter-olympics-check-out-jack-gowers-schedule-4237649"},"dateCreated":"2022-02-04T12:00:40+00:00","datePublished":"2022-02-04T12:00:40+00:00","dateModified":"2022-02-02T15:08:29+00:00","thumbnailUrl":"https:\/\/images.website.ie\/uploads\/2022\/01\/24153939\/Jack-Gower-cropped.jpg","image":{"@type":"ImageObject","url":"https:\/\/images.website.ie\/uploads\/2022\/01\/24153939\/Jack-Gower-cropped.jpg","width":700,"height":370},"articleSection":"Sport","keywords":"Jack Go","author":[{"@type":"Person","name":"Kevin McCart"}],"publisher":{"@type":"Organization","name":"The Southern Star","logo":{"@type":"ImageObject"}}}</script>
任何帮助将不胜感激。
谢谢,
杰拉德
一旦你到达脚本标签,nokogiri 的工作就完成了,是时候解析了 JSON。
require 'json'
def get_kevin(script_element)
data = JSON.parse(script_element.text)
data.fetch("author").first.fetch("name")
end