PHP实现抓取百度搜索结果页面【相关搜索词】并存储到txt文件示例_...

PHP实现抓取百度搜索结果页面【相关搜索词】并存储到txt文件示例_...

2023年7月9日发(作者:)

PHP实现抓取百度搜索结果页⾯【相关搜索词】并存储到txt⽂件⽰例本⽂实例讲述了PHP实现抓取百度搜索结果页⾯【相关搜索词】并存储到txt⽂件。分享给⼤家供⼤家参考,具体如下:⼀、百度搜索关键词【】【】搜索链接搜索结果部分源代码:

⼆、抓取并保存本地

源代码:

o_String=new StringEx(); } public function getItem($word){ $url = "/s?wd=".$word; // 构造包头,模拟浏览器请求 $header = array ( "Host:", "Content-Type:application/x-www-form-urlencoded",//post请求 "Connection: keep-alive", 'Referer:', 'User-Agent: 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 ); //输出结果echo $content; $this->o_String->string=$content; $s_begin='
'; $s_end='
'; $summary=$this->o_String->getPart($s_begin,$s_end); $s_begin='
相关搜索
'; $s_end='
'; $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); //获取搜索内容 $pat = '/(.*?)/i'; preg_match_all($pat, $str, $m); //print_r($m[4]); 链接⽂字 $con = implode(",", $m[4]); //⽣成⽂件夹 $dates = date("Ymd"); $path="./Search/".$dates."/"; if(!is_dir($path)){ mkdir($path,0777,true); } //⽣成⽂件 $file = fopen($("UTF-8","GBK",$q).".txt",'w'); if(fwrite($file,$con)){ echo $con; echo ''; }else{ echo ''; } fclose($file);}>: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; } 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; }}>更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》希望本⽂所述对⼤家PHP程序设计有所帮助。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688893815a181772.html

相关推荐

发表回复

评论列表(0条)

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信