PHP crawl Baidu Search results page of "related search words" and stored

Source: Internet
Author: User
This article describes the content of the PHP crawl Baidu search results page "related search words" and storage, has a certain reference value, now share to everyone, have the need for friends can refer to

First, Baidu search key words "cicada shell company Transfer"

"Cicada shell company Transfer" Search link
Https://www.baidu.com/s?wd=%E7%9F%A5%E4%BA%86%E5%A3%B3%E5%85%AC%E5%8F%B8%E8%BD%AC%E8%AE%A9

* * Search Results Part source code **<p id= "RS" ><p class= "tt" > Related search </p><table cellpadding= "0" ><tbody><tr ><th><a href= "/s?wd=%e5%85%ac%e5%8f%b8%e8%bd%ac%e8%ae%a9%e6%b5%81%e7%a8%8b%e7%9f%a5%e4%ba%86%e5%a3% B3&rsp=0&f=1&oq=%e7%9f%a5%e4%ba%86%e5%a3%b3%e5%85%ac%e5%8f%b8%e8%bd%ac%e8%ae%a9&tn=baiduhome_ pg&ie=utf-8&rsv_idx=2&rsv_pq=88c7804a0000c417&rsv_t=b5f3jkjisj6nkp61k% 2bmmvgeev7up95o1hsjtuois2xv4ssmzxvoovf%2bazavoihb%2bt3bg&rqlang=cn&rsv_ers=xn0&rs_src=0&rsv_pq =88C7804A0000C417&AMP;RSV_T=B5F3JKJISJ6NKP61K%2BMMVGEEV7UP95O1HSJTUOIS2XV4SSMZXVOOVF%2BAZAVOIHB%2BT3BG "> Company transfer Process Cicada shell </a></th>..........<th><a href= "/s?wd=%e7%9f%a5%e4%ba%86%e5%a3%b3%e5%85%ac%e5%8f% B8%e6%b3%a8%e5%86%8c&rsp=8&f=1&oq=%e7%9f%a5%e4%ba%86%e5%a3%b3%e5%85%ac%e5%8f%b8%e8%bd%ac%e8%ae%a9 &tn=baiduhome_pg&ie=utf-8&rsv_idx=2&rsv_pq=88c7804a0000c417&rsv_t=b5f3jkjisj6nkp61k% 2bmmvgeev7up95o1hsjtuois2xv4ssmzxvoovf%2bazavoihb%2bt3bg&rqlang=cn&rsv_ers=xn0&rs_src=0&rsv_pq=88c7804a0000c417&rsv_t= B5F3JKJISJ6NKP61K%2BMMVGEEV7UP95O1HSJTUOIS2XV4SSMZXVOOVF%2BAZAVOIHB%2BT3BG "> Cicada shell Company Registration </a></th> </tr></tbody></table></p>


Second, crawl and save the Local

Source

index.php------------<form action= "index.php" method= "POST" ><input name= "Q" type= "text"/><input type = "Submit" value= "Get Keywords"/></form><?phpheader (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK '); class    combaike{private $o _string=null; Public Function __construct () {include (' CLS.        Stringex.php ');    $this->o_string=new Stringex ();        The Public Function GetItem ($word) {$url = "http://www.baidu.com/s?wd=". $word; Constructs the header, simulates the browser request $header = Array ("Host:www.baidu.com", "content-type:application/x-www-form- Urlencoded ",//post Request" connection:keep-alive ", ' referer:http://www.baidu.com ', ' User-agen t:mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0;        Bidubrowser 2.6) ');        $ch = Curl_init ();        curl_setopt ($ch, Curlopt_url, $url);        curl_setopt ($ch, Curlopt_httpheader, $header); curl_setopt ($ch, Curlopt_returntransfer, 1);        $content = curl_exec ($ch); if ($content = = FALSE) {echo "Error:".        Curl_error ($ch);        } curl_close ($ch);        The result of the output echo $content;        $this->o_string->string= $content;        $s _begin= ' <p id= "rs" > ";        $s _end= ' </p> ';        $summary = $this->o_string->getpart ($s _begin, $s _end);        $s _begin= ' <p class= "tt" > Related search </p><table cellpadding= "0" ><tr><th> ";        $s _end= ' </th></tr></table></p> ';        $content = $this->o_string->getpart ($s _begin, $s _end);    return $content;        } public Function __destruct () {unset ($this->o_string);    }}if ($_post) {$com = new combaike ();    $q = $_post[' Q ']; $str = $com->getitem ($q); Get search Content $pat = '/<a (. *?) Href= "(. *?)" (.*?) > (. *?)         <\/a>/i ';        Preg_match_all ($pat, $str, $m); Print_r ($m [4]);    Link text $con = implode (",", $m [4]);    Generate Folder $dates = Date ("Ymd"); $patH= "./search/". $dates. "    /";     if (!is_dir ($path)) {mkdir ($path, 0777,true); }//Generate file $file = fopen ($path. Iconv ("UTF-8", "GBK", $q).    TXT ", ' w ');        if (fwrite ($file, $con)) {echo $con;    Echo ' <script>alert ("Success") </script> ';    }else{Echo ' <script>alert ("error") </script> '; } fclose ($file);}? >cls. stringex.php-------------<?phpheader (' content-type:text/html; Charset=utf-8 '); class stringex{Public $string = '    ;    Public function __construct ($string = ") {$this->string= $string;        Public function Preggetpart ($s _begin, $s _end) {$s _begin==preg_quote ($s _begin);        $s _begin=str_replace ('/', ' \ \ ', $s _begin);        $s _end=preg_quote ($s _end);        $s _end=str_replace ('/', ' \ \ ', $s _end); $pattern = '/'. $s _begin. ' (.*?)'. $s _end. '        /';        $result =preg_match ($pattern, $this->string, $a _match);        if (! $result) {return $result; }else{return Isset ($a _match[1])? $a _match[1]:'';        }} public Function Strstrgetpart ($s _begin, $s _end) {$string =strstr ($this->string, $s _begin);        $string =strstr ($string, $s _end,true);        $string =str_replace ($s _begin, ", $string);        $string =str_replace ($s _end, ", $string);    return $string;        The public function GetPart ($s _begin, $s _end) {$result = $this->preggetpart ($s _begin, $s _end);        if (! $result) {$result = $this->strstrgetpart ($s _begin, $s _end);    } return $result; }}?>

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.