美文网首页
PHP处理文本和爬虫技巧

PHP处理文本和爬虫技巧

作者: VeyronC | 来源:发表于2017-01-07 16:23 被阅读0次
  • 用 php 处理注意,explode()拆分字符串,返回给一个变量便是将拆分出的东西依次存进这个变量,返回给一个list(变量1,变量2,……)则是将拆分出的东西分别存到变量1,变量2,……。
  • 结合 array_fliter() 过滤的时候,如果不给回调函数过滤不干净的时候,可以在增加过滤回调函数,排除 "\r\n" 和 原本的 FALSE(不写过滤回调函数的时候默认就是这个FALSE)。
  • 调试可用file_put_contents("/home/search/result", var_export(['time'=> date('Y-m-d H:i:s'),'res'=>$result], true)."\n", FILE_APPEND);
<?php
    $file=fopen("mission2.txt","r") or exit("Unable to open file!");
    $fileCity=fopen("city2.txt","r") or exit("Unable to open file!");
    $arr = [];  //用不用都行
    // 读取文件每一行,直到文件结尾
    while(!feof($file))
    {
        //echo fgets($file). "<br>";
        $arr[] = fgets($file);
    }
    while(!feof($fileCity))
    {
        $city[] = fgets($fileCity);
    }
    //print_r($arr);
    
    foreach($arr as $a){
        list($term[]) = explode("   ",$a);
    }
    echo '<br>';
    print_r($term);
    echo '<br>';
    
    $city = array_filter($city);
    
    print_r($city);
    
    function gl($gg){
        if($gg == "\r\n" or $gg == FALSE)   //要用\r\n这个才能过滤掉某些windows里面的换行,还需要加上默认的FALSE,不然也可能滤不干净
            return 0;
        else
            return 1;
    }
    
    foreach($city as $c){                   
        $cityAlone[] = array_filter(explode("\t",$c),"gl");   //array_filter不使用第二个参数回调函数的话默认是过滤FALSE
    }
        
    echo '<br>';
    echo '<pre>';
    print_r($cityAlone);
    echo '</pre>';
    echo '<br>';
    print_r(array_filter($cityAlone[0]));
        
    foreach($term as $key=>$te){
        foreach(array_filter($cityAlone[$key]) as $ci){
            echo $ci.$te.'<br>';    
        }   
    }
    
    fclose($fileCity);
    fclose($file);
?>
<?php
    ini_set('memory_limit', '512M');
    $fileLeimu = fopen("leimu.txt","r") or exit("Unable to open file!");
    $fileCity = fopen("city.txt","r") or exit("Unable to open file!");
    $fileZaci1 = fopen("zaci1.txt","r") or exit("Unable to open file!");
    
    while(!feof($fileLeimu))
    {
        $leimu[] = fgets($fileLeimu);
    }
    while(!feof($fileCity))
    {
        $city[] = fgets($fileCity);
    }
    while(!feof($fileZaci1))
    {
        $zaci1[] = fgets($fileZaci1);
    }
    
    function gl($gg){
        if($gg == "\r\n" or $gg == FALSE)   //要用\r\n这个才能过滤掉某些windows里面的换行,还需要加上默认的FALSE,不然也可能滤不干净
            return 0;
        else
            return 1;
    }
    
    
    $leimu = array_filter($leimu,"gl");
    $city = array_filter($city,"gl");
    $zaci1 = array_filter($zaci1,"gl");
    $zaci2 = ['上门','找'];
    
    $leimu = array_unique($leimu); //去重
    $city = array_unique($city);  
    
    $leimu = str_replace(array("\r\n", "\r", "\n"), "", $leimu);   //去掉每个字符串元素末尾的换行
    $city = str_replace(array("\r\n", "\r", "\n"), "", $city);
    $zaci1 = str_replace(array("\r\n", "\r", "\n"), "", $zaci1);
    
    // echo '<pre>';
    // print_r($zaci2);
    // print_r($leimu);
    // print_r($city);
    // print_r($zaci1);
    // echo '</pre>';

/*  
    foreach($city as $c){
        foreach($leimu as $l){
            foreach($zaci1 as $zOne){
                $combRes[] = $c.$l.$zOne;
            }
        }
    }
    
    foreach($city as $c){
        foreach($leimu as $l){
            foreach($zaci2 as $zTwo){
                $combRes[] = $c.$zTwo.$l;
            }
        }
    }
    
    foreach($city as $c){
        foreach($leimu as $l){
            $combRes[] = $c.$l;
        }
    }
*/
    foreach($leimu as $l){
        foreach($zaci1 as $zOne){
            $combRes[] = $l.$zOne;
        }
    }

    foreach($zaci2 as $zTwo){
        foreach($leimu as $l){
            $combRes[] = $zTwo.$l;
        }
    }
    
    foreach($leimu as $l){
        $combRes[] = $l;
    }
    
    $combRes = implode("\r\n", $combRes);  //想要打印到txt中,能有换行的效果,需要添加这个
    file_put_contents("456.txt",$combRes);
    // echo gettype($combRes);
    echo '<pre>';
    print_r($combRes);
    echo '</pre>';
    
    fclose($fileLeimu);
    fclose($fileCity);
    fclose($fileZaci1);

获取页面某标签中的内容,若是能借助下载simplehtmldom类打开操作的话,dom方便。若打不开,则用file_get_contents或者curl(网上说效率更高),读取全文内容,然后用正则匹配来做。

<?php
    require_once "../classes/simplehtmldom_1_5/simple_html_dom.php";
    $mainHtml = 'http://***/index.xml';
    //$mainHtml ='http://***/20161222-0.xml';  //为何没法用file_get_html打开? 
    $html = file_get_html($mainHtml);   //创建一个DOM
    foreach($html->find('loc') as $loc){
        $locTextRecord[] = $loc->plaintext;
    }
     
     // $htmltest = file_get_contents($locTextRecord[4]);
     
    foreach($locTextRecord as $everyLoc){
        $htmltest = file_get_contents($everyLoc);
     
        $reg = '/\<catg\>(.*?)\<\/catg\>/is';
        if(preg_match_all($reg, $htmltest, $arr)) {
            foreach($arr[1] as $a){
                
                $record[] = $a;
            }
        } else {
            echo "匹配失败!<br>";
        }
    }
    $record = array_unique($record);
    foreach($record as $r){
        var_dump($r);   //利用 var_dump 来查看变量类型,可以调试和直接在网页上复制用。
    }
    $html->clear();
?>

一个主xml下含很多<loc>子标签,在所有xml中,查找某个字符串:

<?php
    $mainHtml = file_get_contents('http://***/tp_index.xml');
    $reg = '/\<loc\>(.*?)\<\/loc\>/is';
    if(preg_match_all($reg, $mainHtml, $arr)) {
        foreach($arr[1] as $a){
            $locTextRecord[] = $a;
        }
    } else {
        echo "loc匹配失败!<br>";
    }
    //var_dump($locTextRecord);
     
    foreach($locTextRecord as $everyLoc){
        $htmlTest = file_get_contents($everyLoc);

        //$aim = 'https://baidu.com?srcid%3D1000%26id%3D10034_5467_%E5%8C%97%E4%BA%AC';
        $aim = '搬家';
        if(strpos($htmlTest, $aim)) {
            $record[] = $everyLoc;
        } else {
            echo "aim匹配失败!<br>";
        }
    }
    //$record = array_unique($record);
    foreach($record as $r){
        var_dump($r);   //利用 var_dump 来查看变量类型,可以调试和直接在网页上复制用。
    }
?>

注意:
preg_match()的第三个参数:如果提供了参数matches,它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推。
preg_match_all()的第三个参数:多维数组,作为输出参数输出所有匹配结果, 数组排序通过flags指定。

相关文章

  • PHP处理文本和爬虫技巧

    用 php 处理注意,explode()拆分字符串,返回给一个变量便是将拆分出的东西依次存进这个变量,返回给一个l...

  • 没有深入只有浅出的PHP

    PHP(超文本预处理器) PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本...

  • Udemy网站课程9-PHP

    PHP维基简介 PHP(全称:PHP:Hypertext Preprocessor,即“PHP:超文本预处理器”)...

  • php01—入门

    1.什么是php? 官方解释:php(PHP Hypertext Preprocessor)又叫php超文本预处理...

  • PHP定义

    什么是PHP? 定义:PHP(外文名:PHP: HypertextPreprocessor,中文名:“超文本预处理...

  • Goutte基本用法

    最近工作上用到PHP爬虫框架Goutte(号称是PHP上最好用的爬虫框架)。这里记下自己用到过的使用技巧,免得下次...

  • PHP七天系列之基础知识

    基础入门 PHP是什么? PHP(PHP: Hypertext Preprocessor,超文本预处理器的缩写),...

  • Hello PHP(1)

    ##PHP(超文本预处理器) 1.PHP(外文名:PHP: Hypertext Preprocessor,中文名:...

  • PHP开发环境搭建

    PHP简介 PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是...

  • 1.什么是php?

    1.什么是php? php(PHP Hypertext Preprocessor)又叫php超文本处理器,是运行在...

网友评论

      本文标题:PHP处理文本和爬虫技巧

      本文链接:https://www.haomeiwen.com/subject/qlufbttx.html