Home  >  Article  >  Backend Development  >  jquery+thinkphp implements cross-domain data crawling method

jquery+thinkphp implements cross-domain data crawling method

高洛峰
高洛峰Original
2017-01-11 11:09:401219browse

The example in this article describes the method of jquery+thinkphp to achieve cross-domain data capture. I would like to share it with you for your reference. The details are as follows:

Today I will make a remote data capture function. Remember that jquery can be used to remotely capture data using Ajax, but it cannot cross domains. I found a lot online. But I think it is better to do a comprehensive one, so now I feel that it has complicated a simple problem, but at least it has been solved for now:

The effect of grabbing data across domains into the local database and then updating it asynchronously

The way I implement it: jquery's $.post sends data to the server background, and the PHP code in the background performs remote fetching, saves it to the database, ajax returns the data to the front desk, and the front desk uses JS to accept the data and display it.

//远程抓取获取数据
 $("#update_ac").click(function() {
      $username = $("#username").text();
      $("#AC,#rank,#Submit,#solved,#solved2,#solved3").ajaxStart(function(){
        $(this).html("   ");
      });
      $.post("update_ac/username/"+$username,{},function($data){
        json = eval("(" + $data + ")");
        $("#Submit").html(json.data.Submit);
        $("#AC").html(json.data.AC);
        $("#solved,#solved2,#solved3").html(json.data.solved);
        $("#rank").html(json.data.rank);
       }
      ),"json";
});

The above jquery code is relatively clear on the fourth floor. What bothers me is the reception of json data

json = eval("(" + $data + ")");
//eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。

In fact, this is all Or the front desk, cross-domain crawling is done using the PHP extension simple_html_dom (if you are not sure, you can search online, it is developed based on PHP5)

crawl the remote page to the local.

import("@.ORG.simple_html_dom");
//thinkphp内导入扩展,你要把网上下载的代码改名为simple_html_dom.class.php放到APPNAME\Lib\ORG的目录下面
$html = file_get_html('http://openoj.awaysoft.com/JudgeOnline/userinfo.php?user='.$username); //远程抓取了
$ret = $html->find('center',0)->plaintext; //返回数据了。

The above code is just the core code, simple_html_dom has many extended functions. Find out for yourself.

The returned data is a string, and then use regular expressions to filter the required data. The following is the rendering

jquery+thinkphp implements cross-domain data crawling method

I hope this article will help Everyone’s PHP programming based on the ThinkPHP framework is helpful.

For more related articles on how jquery+thinkphp implements cross-domain data capture, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn