且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Nokogiri和Ruby解析JavaScript

更新时间:1970-01-01 07:57:48

如果我没看错,您正在尝试解析JavaScript并使用您的图片网址获取Ruby数组,是吗?

If I read you correctly you're trying to parse the JavaScript and get a Ruby array with your image URLs yes?

Nokogiri仅解析HTML/XML,因此您将需要一个不同的库;粗略的搜索会显示 RKelly 库,该库具有 parse 函数,该函数需要JavaScript字符串并返回解析树.

Nokogiri only parses HTML/XML so you're going to need a different library; A cursory search turns up the RKelly library which has a parse function that takes a JavaScript string and returns a parse tree.

一旦有了一个解析树,您将需要遍历它并按名称找到感兴趣的节点(例如 _arPic ),然后在分配的另一端获取字符串内容.

Once you have a parse tree you're going to need to traverse it and find the nodes of interest by name (e.g. _arPic) then get the string content on the other side of the assignment.

或者,如果不必太健壮(也不一定),则可以使用正则表达式来搜索JavaScript:

Alternatively, if it doesn't have to be too robust (and it wouldn't be) you can just use a regex to search the JavaScript if possible:

/^\s*_arPic\[\d\] = "(.+)";$/

可能是一个很好的入门正则表达式.

might be a good starter regex.