gpt4 book ai didi

ruby - 使用 Nokogiri 从链接获取 href 时无法将字符串转换为整数

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:58 25 4
gpt4 key购买 nike

我正在尝试使用 Ruby 和 Nokogiri 解析这个网站:

这是我的代码:

require 'nokogiri'
require 'open-uri'

class StreamsController < ApplicationController
def index
end

def updateall
doc = Nokogiri::HTML(open('http://www.own3d.tv/game/League+of+Legends'))

# Grab all the Live streams from the front page.
doc.css('div#top_live .VIDEOS-1grid-box').each do |stream|
s = Stream.new

# Parse the URL.
s.url = stream.css('a.small-tn')['href']
end
end
end

# Parse the URL 位,我收到错误 Cannot convert String to Integer.

我对如何将 Nokogiri 用于这个简单的用例感到困惑。

如何获取每个 |stream| 对象中每个链接的 href 属性?

最佳答案

问题是 stream.css 返回 NodeSet (如数组)匹配项,因此无法将字符串转换为数组索引。

要获得第一个匹配项,请使用 stream.at_css,这就是我认为您想要的。

关于ruby - 使用 Nokogiri 从链接获取 href 时无法将字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com