如何使用Phantomjs的cookie API? [英] How to use Phantomjs' cookie API?

查看:1552
本文介绍了如何使用Phantomjs的cookie API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从phantomjs的exmaple文件夹中使用rasterize.js将网页捕获为PDF。我遇到的问题是,网页需要用户登录。然后我看到有一个cookie选项Phantomjs。

I am trying to use rasterize.js from the phantomjs' exmaple folder to capture a web page into PDF. The problem I am having is that the web page requires user log in. Then I saw there is a cookie option for Phantomjs. The web site is Django powered just in case it matters.

所以我登录了网站,打开了firebug,输入了 document.cookie 在firebug。我得到了cookie设置,并放入我的cookies.txt。

So I logged into the site manfully, opened the firebug, and typed document.cookie in firebug. I got the cookie settings and put into my cookies.txt.

[http://localhost:7000]
csrftoken: f3da886168fae33b840e7f6c93240dff
sessionid: 27e90c3214b0ec94dadc739665724708
django_language: en

rasterize.js以创建如下所示的pdf:

Then I used the rasterize.js to create the pdf like below:

phantomjs --cookies-file=cookies.txt examples/rasterize.js http://localhost:7000/reports /tmp/report.pdf

但结果还是日志

推荐答案

您是否已尝试在PhantomJS中自动执行登录过程,确保包含cookie param?这样phantomjs可以验证用户就像浏览器,使用cookie。

Have you tried automating the login process in PhantomJS, making sure to include cookies param? This way phantomjs can authenticate a user just like a browser would, using cookies.

另一种选择是使用像fiddler这样的代理来检查请求,比较firefox的工作请求和未经验证的phantomjs发出的请求。

Another option would be to inspect the requests using a proxy like fiddler, compare the request made from firefox that works and the request made from phantomjs that is not authenticated. The information in the header should tell you what you are missing.

您可以使用phantomjs提供的功能来添加Cookie:

You can add cookies by using the function phantomjs provides:

http://phantomjs.org/api/webpage/method/add -cookie.html

这篇关于如何使用Phantomjs的cookie API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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