首页 > 解决方案 > 如何为爬虫设置cookie

问题描述

在本站中,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.

然后,我在 php 脚本中使用file_get_contents.

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

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

标签: javascriptphpjquery

解决方案


这给了我答案。

使用 file_get_contents 发送 cookie

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

$context = stream_context_create($opts);

推荐阅读