如何为爬虫设置cookie

时间:2021-06-28 11:18:41

标签: javascript php jquery

在本网站中,需要点击 Yes 次才能进入页面。

<a id="ok" href="./">Yes</a>
<script>
$('#ok').bind("click", function(e) {
    e.preventDefault();
    var min=60*24*7;
    var dt=new Date();
    dt.setTime(dt.getTime()+(min*60*1000));
    $.cookie('check','1'{domain:'exmaple.com',path:'/',expires:dt});
    window.location.reload();
}
</script>

现在我检查了来源,并了解到它需要 cookie 作为 check=1

然后,我使用 file_get_contents 在 php 脚本中像这样爬行。

$url = "https://www.example.com";
$source = file_get_contents($url,false,$context);

如何设置cookie? 或者有可能吗??

1 个答案:

答案 0 :(得分:0)

这给了我答案。

Send cookie with file_get_contents

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);
相关问题