TypeError: can't cast Nokogiri::XML::Attr to string

TypeError: can't cast Nokogiri::XML::Attr to string

我正在尝试抓取一些数据并查找或创建一个 Rails 对象,代码如下:

@books = @page.xpath(BOOKS_SELECTOR).map { |book| Book.find_or_create_by(name: book.attribute('title'))}

但是当这段代码运行时我得到这个错误:

  1) Error:
ScrapeHelperTest#test_scrape_page_test:
TypeError: can't cast Nokogiri::XML::Attr to string
    app/helpers/scrape_helper.rb:25:in `block in scrape'
    app/helpers/scrape_helper.rb:25:in `map'
    app/helpers/scrape_helper.rb:25:in `scrape'
    test/helpers/scrape_helper_test.rb:11:in `block in <class:ScrapeHelperTest>'
@books = @page.xpath(BOOKS_SELECTOR).map do |book| 
  Book.find_or_create_by(name: book['title'])
end

在节点上使用散列访问器 [] returns 属性值而不是 #attribute.

返回的 Nokogiri::XML::Attr 节点