You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求R Kelly Remix gem规范文档及Nokogiri提取页面JS指导

Hey there! Let's tackle your two requests one by one—first, putting together a solid spec doc for the R Kelly Remix gem in Ruby on Rails, then walking through how to extract that Amazon JavaScript snippet using Nokogiri.

R Kelly Remix Gem规范文档(适用于Ruby on Rails)

1. 安装

  • 在Rails项目的Gemfile中添加:
    gem 'r_kelly_remix'
    
  • 执行安装命令:
    bundle install
    

2. 核心功能概述

R Kelly Remix is a Ruby library built for parsing, manipulating, and generating JavaScript code. In Rails projects, it’s commonly used for:

  • Static analysis of frontend JavaScript code
  • Dynamically generating JS snippets that follow specific specs
  • Extracting and parsing embedded JS data from HTML pages (like the Amazon image data you referenced)

3. 基本用法示例

解析JavaScript代码

require 'r_kelly_remix'

# Initialize parser
parser = RKellyRemix::Parser.new
# Sample JS code matching your Amazon snippet structure
js_code = "var data = { 'colorImages': { 'initial': [...] } };"
# Parse into Abstract Syntax Tree (AST)
ast = parser.parse(js_code)

# Traverse AST to find the 'data' assignment
ast.each_node do |node|
  if node.is_a?(RKellyRemix::Nodes::AssignNode) && node.left.value == 'data'
    puts "Found data object: #{node.right.to_ruby}"
  end
end

在Rails Controller中集成

class ProductsController < ApplicationController
  def extract_amazon_images
    # Assume we've already fetched the target JS snippet via Nokogiri
    js_script = params[:js_script]
    parser = RKellyRemix::Parser.new
    ast = parser.parse(js_script)

    # Extract the colorImages data from the AST
    data_node = ast.find { |n| n.is_a?(RKellyRemix::Nodes::AssignNode) && n.left.value == 'data' }
    @image_data = data_node.right.to_ruby if data_node

    render json: @image_data
  end
end

4. 注意事项

  • Ensure your Rails version is compatible (supports Rails 5.x and above)
  • For complex JS code, extract the target snippet first with Nokogiri before parsing with R Kelly Remix—this avoids performance issues from parsing the entire page's JS
  • For minified JS, use tools like uglifier to format it first, which improves parsing accuracy
使用Nokogiri提取页面中的JavaScript代码(以亚马逊图片脚本为例)

1. 解析HTML页面

First, fetch and parse the target Amazon page with Nokogiri:

require 'nokogiri'
require 'open-uri'

# Replace with your target product URL
target_url = "https://www.amazon.com/your-target-product-page"
doc = Nokogiri::HTML(URI.open(target_url))

2. 定位目标JavaScript脚本

Locate the script tag using unique content identifiers (like ImageBlockATF or colorImages):

target_script = doc.css('script').find do |script|
  script.content.include?("P.when('A').register(\"ImageBlockATF\"")
end

3. 提取并解析JSON数据

Extract the data object from the script and convert it into a Ruby hash:

if target_script
  # Use regex to isolate the data object
  data_match = target_script.content.match(/var data = ({.*?});/)
  if data_match
    require 'json'
    # Convert JS-style single quotes to JSON-compliant double quotes
    sanitized_json = data_match[1].gsub("'", "\"")
    image_data = JSON.parse(sanitized_json)

    # Access and use the image data
    initial_images = image_data['colorImages']['initial']
    initial_images.each do |img|
      puts "Thumbnail URL: #{img['thumb']}"
      puts "Large image URL: #{img['large']}"
    end
  end
end

4. 优化技巧

  • If multiple similar scripts exist, narrow down the search using script id attributes or more specific regex patterns
  • For dynamically loaded JS, use tools like mechanize or capybara to simulate browser rendering, ensuring you capture fully loaded scripts

内容的提问来源于stack exchange,提问作者Hussain Niazi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:05:31