First, the source of the collection class
Copy CodeThe code is as follows:
/**
* Universal List Acquisition Class
* Version V1.3
* Author: JAE
*/
Require_once '. /phpquery/phpquery/phpquery.php ';
Class querylist{
Private $pageURL;
Private $REGARR = Array ();
Public $JSONARR = Array ();
Private $regRange;
Private $html;
/************************************************
* Parameters: Page Address selector array block selector
* "Selector array" description: Format Array ("Name" =>array ("selector", "type"),.......)
* "Type" description: Value "text", "HTML", "Properties"
* "Block selector": refers to the rule to select a few chunks, and then separately in the block to make relevant choices
*************************************************/
function Querylist ($pageURL, $regArr =array (), $regRange = ")
{
$this->pageurl = $pageURL;
In order to get https://
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $this->pageurl);
curl_setopt ($ch, Curlopt_ssl_verifypeer, false);
curl_setopt ($ch, Curlopt_ssl_verifyhost, false);
curl_setopt ($ch, curlopt_returntransfer,1);
$this->html = curl_exec ($ch);
Curl_close ($ch);
if (!empty ($REGARR))
{
$this->regarr = $REGARR;
$this->regrange = $regRange;
$this->getlist ();
}
}
function Setquery ($REGARR, $regRange = ")
{
$this->jsonarr=array ();
$this->regarr = $REGARR;
$this->regrange = $regRange;
$this->getlist ();
}
Private Function GetList ()
{
$hobj = phpquery::newdocumenthtml ($this->html);
if (!empty ($this->regrange))
{
$robj = PQ ($hobj)->find ($this->regrange);
$i = 0;
foreach ($robj as $item)
{
while (list ($key, $reg _value) =each ($this->regarr))
{
$iobj = PQ ($item)->find ($reg _value[0]);
Switch ($reg _value[1])
{
Case ' text ':
$this->jsonarr[$i] [$key] = Trim (PQ ($iobj)->text ());
Break
Case ' HTML ':
$this->jsonarr[$i] [$key] = Trim (PQ ($iobj)->html ());
Break
Default
$this->jsonarr[$i] [$key] = PQ ($iobj)->attr ($reg _value[1]);
Break
}
}
Resetting an array pointer
Reset ($this->regarr);
$i + +;
}
}
Else
{
while (list ($key, $reg _value) =each ($this->regarr))
{
$lobj = PQ ($hobj)->find ($reg _value[0]);
$i = 0;
foreach ($lobj as $item)
{
Switch ($reg _value[1])
{
Case ' text ':
$this->jsonarr[$i ++][$key] = Trim (PQ ($item)->text ());
Break
Case ' HTML ':
$this->jsonarr[$i ++][$key] = Trim (PQ ($item)->html ());
Break
Default
$this->jsonarr[$i ++][$key] = PQ ($item)->attr ($reg _value[1]);
Break
}
}
}
}
}
function Getjson ()
{
Return Json_encode ($this->jsonarr);
}
}
Ii. Examples of Use
Copy the Code code as follows:
Require ' query/querylist.class.php ';
Acquisition of the OSC code share list, Title link author
$url = "Http://www.oschina.net/code/list";
$reg = Array ("title" =>array (". Code_title a:eq (0)", "text"), "url" =>array (". Code_title a:eq (0)", "href"), "author "=>array (" img "," title "));
$rang = ". Code_list li";
$HJ = new Querylist ($url, $reg, $rang);
$arr = $hj->jsonarr;
Print_r ($arr);
If you want to take the TOP40 active contributor image to the right of the current page, get the JSON data, you can write this
$reg = Array ("Portrait" =>array (". Hot_top img", "src"));
$hj->setquery ($reg);
$json = $hj->getjson ();
Echo $json. "
";
Content page content of OSC
$url = "http://www.oschina.net/code/snippet_186288_23816";
$reg = Array ("title" =>array (". Qtitle H1 "," text ")," Con "=>array (". Content "," html "));
$HJ = new Querylist ($url, $reg);
$arr = $hj->jsonarr;
Print_r ($arr);
So many examples, is not used to do collection is very convenient
http://www.bkjia.com/PHPjc/751515.html www.bkjia.com true http://www.bkjia.com/PHPjc/751515.html techarticle First, the source code of the collection of code copy codes are as follows:? PHP/** * Generic List Acquisition class * Version V1.3 * Author: JAE * */require_once '. /phpquery/phpquery/phpquery.php '; Class querylist{Priva ...