新手问题 ruby 如何获取网页中 js 运行后写入的 html 代码

Daniel_Xu · 2012年11月27日 · 最后由 windless_j 回复于 2013年02月20日 · 6789 次阅读

我想抓取淘宝的一 list 下的所有内容,然后,每个 link 已经获取,但是,当我用

hpricot 和 httpclient 去获取每个 link 的源码时,puts 出的是 js

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

function htmlspecialchars(str){ str = str.replace(//g, '>'); str = str.replace(/"/g, '"'); str = str.replace(/'/g, '''); return str; } function bol(){ var inf = (top.location!=self.location); var qs = location.search.split("?")[location.search.split("?").length-1].split("&"); qso = {}; for(var i=0;i<qs.length if var tmpa='qs[i].split("=");' qso tu="unescape(qso.tu);" tu.length exit oi='document.createElement("iframe");' oi.id="iobj" oi.border="0;" oi.frameborder="0;" oi.style.height="1px" oi.style.width="1px" document.body.appendchild cd="oi.contentWindow.document;" cd.write><bo style="margin:0px;padding:0px"><scr src="http://js.tongji.linezing.com/1023331/tongji.js" type="text/javascript"></scr><noscr><a href="http://www.linezing.com"><img src="http://img.tongji.linezing.com/1023331/tongji.gif"></a></noscr></bo>'); window.setTimeout(function(){cd.close();if(qso.tu)location.href = unescape(qso.tu);},1000); }else{ if(qso.co&amp;&amp;qso.co!=""){ document.cookie="tk_trace="+(qso.co)+";path=/;domain=.taobao.com"; } if(qso.tu &amp;&amp; (qso.tu.indexOf("http%3A%2F%2Fs.click.taobao.com%2F")===0 || qso.tu.indexOf("http%3A%2F%2Fs.click.alimama.com%2F")===0 || qso.tu.indexOf("http%3A%2F%2Fitem8.taobao.com%2F")===0 || qso.tu.indexOf("http%3A%2F%2Fshop8.taobao.com%2F")===0)){ if(!window.attachEvent){ document.write('<input style="display:none" type="button" id="exe" value="" onclick="window.location=\''+unescape(qso.tu)+'\'">'); document.getElementById('exe').click(); }else{ document.write('<a style="display:none" href="'+unescape(qso.tu)+'" id="exe"></a>'); document.getElementById('exe').click(); } } } }//end of bol() bol(); </qs.length>

我想问下怎么能获取到 js 填充过后的源码,可以让我来搜索字段,谢谢

直接分析 js 在干啥,然后按照逻辑在 ruby 里跟着操作一次即可。

这个问题比较麻烦 我以前一个朋友研究过 如果是我做的话,我会考虑模拟运行 [使用解释器啊] 或者正则

@ywencn @chucai 高手们,能不能再具体点,我是新手,很新的那种!!!

一般来讲爬网页,就获得链接,然后按照字段来取,这次真是遇到麻烦了。

貌似以前看过有个哥们说把这段 js 代码给通过某个 gem 给运行一遍生成出 html 出来,一下不知道哪个帖子放哪去了

#3 楼 @Daniel_Xu 看就看他的运行结果啊。。不就是一堆 js 在操作字符串么《 按照他的逻辑用 Ruby 直接得出结果然后继续啊,又不一定非要原样执行 如果遇到比较变态的算法,可以用 therubyracer,直接执行他的 JS。 举个例子,xici 的网站登陆的时候密码不是明文,是一个 js 算法算过的东西,假如计算的是 password.js 那么

pw = "原来密码"
cxt = V8::Context.new
cxt.load File.expand_path(File.dirname(__FILE__) + "/../public/password.js")
r = cxt.eval(pw)

这样 r 拿到的就是 js 计算的结果了

先分析 js 干嘛的,然后看能否通过 ruby 模拟,(一般都可以)

需要 登录 后方可回复, 如果你还没有账号请 注册新账号