PhantomJS Cookies保留并重复使用后续请求

时间:2017-01-03 21:15:09

标签: cookies phantomjs

我刚刚安装了phantom.js,现在正尝试使用它。

我正在尝试获取网站的内容,但是我需要在继续之前表明我同意他们的隐私政策。

我目前的代码如下:

var page = require('webpage').create();
lastName = "T";
firstName = "N";
mmddyyyyS = "12/03/2016";
mmddyyyyE = "12/04/2016";
page.open("http://example.com/casesearch/processDisclaimer.jis");
page.open("http://example.com/casesearch/inquirySearch.jis?lastName="+lastName+"company=N&firstName="+firstName+"&filingStart="+mmddyyyyS+"&filingEnd="+mmddyyyyE, function(status) {  
    if ( status === "success" ) {
        if (page.injectJs("jquery.min.js")&&page.injectJs("moment.min.js")) {
            var h1 = page.evaluate(function() {
                return $("h1:eq(0)").css({fontSize: 10, color: "red"}).text();
            });
            console.log(h1);
        }
    }
    else{
        console.log("error");
    }
});
phantom.onError = function(msg, trace) {
  var msgStack = ['PHANTOM ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
    });
  }
  console.error(msgStack.join('\n'));
  phantom.exit(1);
};

我总是从console.log("error")收到错误。我相信这是因为我发出的第一个请求表明我接受了他们的条款,但是在提交之后我通常会在浏览器中获得的cookie不会存储在后续(第二个)page.open请求中。我在文档addCookie()page.cookies中看到过,但没有关于链接请求的内容,以便在第二页的请求中使用在一个页面上获得的cookie。

我该怎么做?

谢谢。

1 个答案:

答案 0 :(得分:1)

任何网络请求在PhantomJS中都是异步的,因此第二个public class AlarmReceiver extends BroadcastReceiver { private static int NOTIFICATION_ID = 1; NotificationManager notificationManager; private PendingIntent pendingIntent; @Override public void onReceive(Context context, Intent intent) { String myUserid = new SavedPreferences(context).getUserId(); String notificationUserId = intent.getStringExtra("NotificationUserId"); if (!TextUtils.isEmpty(myUserid) && myUserid.equals(notificationUserId)) { String bookingId = intent.getStringExtra("bookingId"); System.out.println("SP ID IN RECEIVER"+intent.getStringExtra("spId")); String spId = intent.getStringExtra("spId"); if (new Date().getTime() > dbTime) {// Here time is past so need not to play notification. } else {// Notification time is in future. So can play the notification. notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); Intent mIntent = new Intent(context, RatingActivity.class); NOTIFICATION_ID = Integer.parseInt(intent.getStringExtra("Id")); mIntent.putExtra("message", intent.getStringExtra("message")); mIntent.putExtra("Id", NOTIFICATION_ID); mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); pendingIntent = PendingIntent.getActivity(context, 111, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(uri); builder.setAutoCancel(true); builder.setContentTitle("Test"); builder.setContentText(intent.getStringExtra("message")); builder.setSmallIcon(R.drawable.app_icon); builder.setContentIntent(pendingIntent); builder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.service_completed_text))); notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, builder.build()); } } } } 在第一个完成之前就已启动,这就是发出错误的原因。

page.open

另请注意,当使用命令行参数page.open("http://example.com/casesearch/processDisclaimer.jis", function(){ // this block is executed after the first page is opened page.open("http://example.com/casesearch/inquirySearch.jis?lastName="+lastName+"company=N&firstName="+firstName+"&filingStart="+mmddyyyyS+"&filingEnd="+mmddyyyyE, function(status) { // after the second page is opened }) });

运行时,PhantomJS可以保留以前请求中的cookie以供将来使用
--cookies-file