本文介绍了使用 nokogiri 解析 javascript 函数元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 mechanize + nokogiri 解析脚本标记中的值.这是我所能得到的

I'm trying to parse out values within a script tag with mechanize + nokogiri. This is as far as i'm able to get

1.9.3-p125 :107 > agent.page.search("script")[7]
=> #<Nokogiri::XML::Element:0x3ff1e10f3ff8 name="script" attributes=
[#<Nokogiri::XML::Attr:0x3ff1e10f3f80 name="type" value="text/javascript">] children=
[#<Nokogiri::XML::CDATA:0x3ff1e10f39b8 "countdownFactory.create('47884', '1333724400000', '');countdownFactory.create('48436', '1333638000000', '');countdownFactory.create('46085', '1333627200000', '');countdownFactory.create('48151', '1333551600000', '');countdownFactory.create('48211', '1333638000000', '');countdownFactory.create('48511', '1333551600000', '');countdownFactory.create('48513', '1333551600000', '');countdownFactory.create('48482', '1333551600000', '');countdownFactory.create('48439', '1333551600000', '');countdownFactory.create('48299', '1333551600000', '');countdownFactory.create('48272', '1333670400000', '');countdownFactory.create('46371', '1333638000000', '');countdownFactory.create('48254', '1333724400000', '');countdownFactory.create('46086', '1333638000000', '');countdownFactory.create('48317', '1333638000000', '');countdownFactory.create('48435', '1333724400000', '');countdownFactory.create('47223', '1334059200000', '');countdownFactory.create('48234', '1333972800000', '');countdownFactory.create('48407', '1333638000000', '');countdownFactory.create('48429', '1333638000000', '');countdownFactory.create('48212', '1333638000000', '');countdownFactory.create('48275', '1333551600000', '');countdownFactory.create('48205', '1333551600000', '');countdownFactory.create('48414', '1333886400000', '');countdownFactory.create('48185', '1333713600000', '');countdownFactory.create('48215', '1333540800000', '');countdownFactory.create('47636', '1333638000000', '');">]

我如何将所有 countdownFactory.create 元素放入一个 Hash 中?谢谢!

How do i get all the countdownFactory.create elements into a Hash? Thanks!

推荐答案

Nokogiri 不做 JavaScript 解析,但是用正则表达式解析这并不难:

Nokogiri doesn't do JavaScript parsing, but this is not too hard to parse with a regular expression:

element = agent.page.search("script")[7]
text = element.text # not 100% sure on this line. Just need the script text though.
Hash[text.scan(/countdownFactory.create\('(\d+)', '(\d+)', ''\)/)]

这篇关于使用 nokogiri 解析 javascript 函数元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:52