一次对HTTPS页面抓取的报错发现过程

今天发现系统后台的某个抓取页面突然失效了,提示:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

大概意思就是,在主线程里使用同步的ajax请求对用户体验有影响,所以不让用了。

于是修改一下抓取函数:

function getProcessData(url)
	{
	    $.ajax({ 
	        type: "get",        //使用get方法访问后台 
	        dataType: "jsonp",  //返回json格式的数据 
	        jsonp:"callback",
	        url: '/news_spider_process/',  // 跨域URL  
	        //url: 'http://localhost/test.php',  // 跨域URL  
	        data:{"url":url},
	        //async: false,
	        //async: true,
	        error: function (jqXHR, exception) {
	            var msg = '';
	            //alert(jqXHR.status);
	            //alert(jqXHR.responseText);
	            if (jqXHR.status === 0) {
	                msg = 'Not connect.\n Verify Network.';
	            } else if (jqXHR.status == 404) {
	                msg = 'Requested page not found. [404]';
	            } else if (jqXHR.status == 500) {
	                msg = 'Internal Server Error [500].';
	            } else if (exception === 'parsererror') {
	                msg = 'Requested JSON parse failed.';
	            } else if (exception === 'timeout') {
	                msg = 'Time out error.';
	            } else if (exception === 'abort') {
	                msg = 'Ajax request aborted.';
	            } else {
	                msg = 'Uncaught Error.\n' + jqXHR.responseText;
	            }
	            //$('#content').html(msg);
	        },
	        success: function(data){
	            //alert(data.url);
	            $("#news_title").val(data.url);
	            //$("#title").html(data.url);
	            //$("#tagA").html("333");
	            re = new RegExp("\/p>","g");
	            $("#tagA").html(data.content.replace(re,"/p>\n"));
	            $("#news_creater").val("nowamagic.net");
	        }
	    }) 
	}

先是把async: false注释掉,发现抓取依然是不行。照理这个是警告,不会阻止程序的运行才对的。

于是加上$.ajax的error选项,发现jqXHR.status输出 200,就是网络是通的。而jqXHR.responseText返回了一处PHP报错,定位到错误处,发现$array file_get_contents($url); 报错了。之前一直都是正常的,怎么突然报错了呢?去那个网页一看,发现网页已经全部用上HTTPS了。

如何让抓取支持HTTPS呢?这里环境是xampp,就以这个为例。

首先,检查/xampp/php/ext目录下是否存在php_openssl.dll文件,一般是有的,没有就需要另行下载。

然后/xampp/php/php.ini文件,查找extension=php_openssl.dll,如果找到了,去掉前面的分号;如果没找到就在extension=php_curl.dll的下一行添加如下代码:extension=php_openssl.dll,然后重启Apache就行了。

打开phpinfo(),查看一下openssl是否已正常启用,当正常启用时,在OpenSSL support后面会出现enabled。

2016_08_20_01.png

或者用下面的语句判断openssl的启用情况:

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', PHP_EOL;
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', PHP_EOL;
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', PHP_EOL;
echo 'wrappers: ', var_export($w);

现在后台抓取又重新正常,问题解决很容易,就是在发现问题上花的时间长了。

转载于:https://my.oschina.net/sunYwp/blog/791829

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值