好吧,我错了,其实这也一样只是 cookie 的问题.作为老司机,我必须再练下手,下面是解决的步骤:
准备工作:ubuntu 系统
1:首先安装 phantomjs ubuntu 系统下 最新版
http://phantomjs.org/download.htmlcd /usr/local/share/
sudo wget
https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2sudo tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-2.1.1-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
which phantomjs
2:获取要被执行的 js, 创建一个 python 文件如下内容:
import requests
import commands
html=requests.get('
https://www.bw.com/pool/btcIndexChartsData?type=2')
cookie1=html.cookies['__jsluid'] #第一个 cookie
js = html.text.replace("<script>", '')
js = js.replace("</script>", '')
js = js.replace("eval", 'console.log')
js = js+'phantom.exit()'
f = open('getcookie.js', "w+")
f.write(js)
f.close()
status, output = commands.getstatusoutput('phantomjs getcookie.js') #第一次解析获得用来生成 cookie 的 js 代码
idx=output.find('{};')
output2=output[idx+3:] #去除头部
idx=output2.find('setTimeout')
output2=output2[0:idx] #去除尾部
output2=output2+'console.log(dc);phantom.exit();'
f = open('getcookie.js', "w+")
f.write(output2)
f.close()
status, cookie2 = commands.getstatusoutput('phantomjs getcookie.js') #第二次执行 js 代码获得 cookie
cookie2=cookie2.replace('__jsl_clearance=','')
#用 cookie1 和 cookie2 去获取真正的数据
cookie = {'__jsluid':cookie1 , '__jsl_clearance':cookie2}
html=requests.get('
https://www.bw.com/pool/btcIndexChartsData?type=2',cookies=cookie)
print html.text