在Ruby上评估JavaScript [英] Evaluate javascript on Ruby

查看:130
本文介绍了在Ruby上评估JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取网页的代码html,但网页包含一些生成一些我需要的数据的JavaScript代码。

  http = Net :: HTTP.new('localhost')
path ='/files.php'

#POST请求 - >登录
data =''
headers = {
'Referer'=> 'http:// localhost:8080 / files.php',
'User-Agent'=> 'Mozilla / 5.0(Windows NT 6.2; WOW64; rv:17.0)Gecko / 20100101 Firefox / 17.0',
'Accept'=> 'text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8',
'Accept-Language'=> 'es-ES,es; q = 0.8,en-US; q = 0.5,en; q = 0.3',
'Content-Encoding'=> 'gzip,deflate',
'Connection'=> 'keep-alive',
'Cookie'=> ''
}

resp,data = http.post(path,data,headers)

puts resp.body

但是这只会返回html而不评估javascript。我想在评估页面的javascript后获取最终的html。

解析方法

在你的页面上。否则,你将不得不通过寻找你想要的每一个js来解析。你需要的宝石称为therubyracer,它将谷歌的v8 javascript执行引擎嵌入到你的ruby中。



转到命令行并用

安装therubyracer b
$ b

  gem install therubyracer 

然后:

  require'v8'

data =''
headers = {
'Referer'=> 'http:// localhost:8080 / files.php',
'User-Agent'=> 'Mozilla / 5.0(Windows NT 6.2; WOW64; rv:17.0)Gecko / 20100101 Firefox / 17.0',
'Accept'=> 'text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8',
'Accept-Language'=> 'es-ES,es; q = 0.8,en-US; q = 0.5,en; q = 0.3',
'Content-Encoding'=> 'gzip,deflate',
'Connection'=> 'keep-alive',
'Cookie'=> ''
}

resp,data = http.post(路径,数据,标题)

js = resp [resp.index('< script' ).. resp.index('< / script>')]
js = js [js.index('/>')..- 1]

cxt = V8 :: Context.new
result = cxt.eval(js)
puts results


I tried get code html of a web page, but the web contains some javascript code that generates some data that I need.

http = Net::HTTP.new('localhost')
path = '/files.php'

# POST request -> logging in
data = ''
headers = {
   'Referer' =>  'http://localhost:8080/files.php',
   'User-Agent' => 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0',
   'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
   'Accept-Language' => 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
   'Content-Encoding' => 'gzip, deflate',
   'Connection' => 'keep-alive',
   'Cookie' => ''
}

resp, data = http.post(path, data, headers)

puts resp.body

But this only returns the html without evaluate the javascript. I would like get the final html after evaluate the javascript of the page.

解决方案

Assumptions made: Your Javascript lives in a single tag on your page. Otherwise you'll have to parse through looking for each bit of js you want. The gem you want is called "therubyracer", it embeds google's v8 javascript execution engine into your ruby.

Go to your command line and install therubyracer with

 gem install therubyracer

then:

 require 'v8'

 data = ''
 headers = {
    'Referer' =>  'http://localhost:8080/files.php',
    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0',
    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language' => 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
    'Content-Encoding' => 'gzip, deflate',
    'Connection' => 'keep-alive',
    'Cookie' => ''
 }

 resp, data = http.post(path, data, headers)

 js = resp[resp.index('<script')..resp.index('</script>')]
 js = js[js.index('/>')..-1]

 cxt = V8::Context.new
 result = cxt.eval(js)
 puts result

这篇关于在Ruby上评估JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆