PHP multithreading pthreads implementation QQ number crawler

Source: Internet
Author: User

Browse through the history of space, crawl out of the person viewing your space (typically restricting 20 people, unless you turn on yellow diamonds), and then crawl out of these 20 people's browsing history and crawl down, you can control the crawl depth.
Here just to give the fear of the code snippet, you can further optimize the QQ classification store. Through QQ Mutual browsing relationship, you can draw a friend network through drawing tools. Wait a minute

Code involves pthreads if not clear read: PHP Advanced programming Multi-Threading
Http://netkiller.github.io/journal/thread.php.html

<?PHP/*Homepage:http://netkiller.github.ioauthor:neo <[email protected]>*/if(!extension_loaded(' Pthreads ')) die(' Please install pthreads '); include_once(' Snoopy.class.php '); classCrawlerworkerextendsWorker {protected  Static $DBH;  Public function__construct () {} Public functionrun () {/*$dbhost = ' db.example.com ';            Database server $dbuser = ' example.com ';                 Database user name $DBPW = ' password ';                Database Password $dbname = ' example '; Database name self:: $DBH = new PDO ("mysql:host= $dbhost;p ort=3306;dbname= $dbname", $dbuser, $DBPW, Array (PDO :: Mysql_attr_init_command = ' SET NAMES \ ' utf8\ ', pdo::mysql_attr_compress = True, pdo::attr    _persistent = true)); */    }    protected functiongetinstance () {returnSelf::$DBH; } } /*The collectable class implements machinery for Pool::collect*/classCrawlerextendsStackable { Public $depth= 3; Private Static $level= 0;  Public function__construct ($QQ) {        $this-&GT;QQ =$QQ; }     Public functionrun () {Try {            $DBH=$this->worker->getinstance (); $this->recursion (Array($this-QQ)); }        Catch(pdoexception$e) {            $error=sprintf("%s,%s\n",$mobile,$id ); file_put_contents("Mobile_error.log",$error,file_append); }        //printf ("Runtime:%s,%s\n", Date (' y-m-d h:i:s '), $this->worker->getthreadid ());        $lst = $this->qzone ($this->qq); Print_r ($LST);    }     Public functionRecursion ($qqs){                 if(Self::$level<=$this-depth) { Self::$level++; }Else if(Self::$level> 0) { self::$level--; }        printf(' Level:%s\n ', Self::$level); //sleep (1);        Usleep(Mt_rand(10000,1000000)); if(Self::$level>=$this-depth) {            return; }                 foreach($qqs  as $uin) {            $lst=$this->qzone ($uin); Print_r($lst); $this->recursion ($lst); }    }      Public functionQzone$QQ){        $url= ' http://m.qzone.com/mqz_get_visitor?g_tk=1191852101&res_mode=0&res_uin= '.$QQ.‘ &offset=0&count=100&page=1&format=json&t=1401762986882&sid= Dodkvcyv6azjn87cxxq5mao1xgakyjhg18c8aa5e0201%3d%3d '; $snoopy=NewSnoopy; //need an proxy?        $snoopy->proxy_host = "My.proxy.host";                  $snoopy->proxy_port = "8080"; Set browser and Referer:        $snoopy->agent = "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) "; $snoopy->referer = "http://m.qzone.com/"; //set Some cookies://$snoopy->cookies["SessionID"] = ' 238472834723489 ';                  $snoopy->cookies["FavoriteColor"] = "blue"; Set an raw-header:        $snoopy->rawheaders["Pragma"] = "No-cache"; //set some internal variables:        $snoopy->maxredirs = 2; $snoopy->offsiteok =false; $snoopy->expandlinks =false; //set username and password (optional)//$snoopy->user = "Joe";                  $snoopy->pass = "Bloe"; Fetch the text of the website www.google.com:        if($snoopy->fetchtext ($url)){             //Other Methods:fetch, Fetchform, Fetchlinks, Submittext and submitlinks//Response code://PR int "Response code:". $snoopy->response_code. "                      <br/>\n ";            Print the headers://print "<b>Headers:</b><br/>"; while (list ($key, $val) = each ($snoopy->headers)) {//Print $key. ":". $val. "            <br/>\n "; }//Print the texts of the website://print_r (Json_decode ($snoopy->results));                         $results=Array(); $tmp= Json_decode ($snoopy-results); if($tmp){                if(Property_exists ($tmp, ' Data ')){                    foreach($tmp->data->List  as $lst ){                        $results[] =$lst-UIn; }                }            }            return($results); }        Else {            Print"Snoopy:error while fetching document:".$snoopy->error. " \ n "; }           }} $pool=NewPool (\crawlerworker::class, []); #foreach (range (100000) as $number) {#$pool->submit (New Crawler ($number));#} $pool->submit (NewCrawler (' 13721218 '));$pool->submit (NewCrawler (' 291379 '));//$pool->submit (new Crawler (' xxx ')),//$pool->submit (new Crawler (' xxx '));//$pool->submit (' New Crawler (' xxx '));//And so on//$pool->submit (New Crawler (' nnn ')); $pool-shutdown ();?>

PHP multithreading pthreads implementation QQ number crawler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.