javascript - 如何为爬虫设置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?或者有可能吗?
解决方案
这给了我答案。
使用 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);
推荐阅读
- python - python列表中N个最大值的位置?
- c# - WPF 从 SystemParameter 单位转换为与设备无关的单位
- c# - 使用 Azure 存储异步创建 blob 时出现异常 - UploadTextAsync
- sql - SELECT子句SQL中出现多个字段时无法对单个字段进行GROUP BY
- html - 防止 div 重叠
- python - 正则表达式仅获取第一个元素-Python
- pentaho - Pentaho Kettle 的 Slack 插件安装失败 - MacOS
- asp.net-mvc-5 - ASP.Net MVC 5 动态添加字段到表单
- android - 尝试编译旧的 Android Native App 时出错
- python - 熊猫使用逻辑或公共列之间合并两个数据框