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

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

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/.php

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

因而修改一下抓取函数:java

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

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

如何让抓取支持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就好了。app

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

或者用下面的语句判断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);

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

相关文章
相关标签/搜索