PHP处理文本和爬虫技巧

  • 用 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是一种可以在服务器端运行的编程语言,可以运行在Web服务器端。 php是一门后台编程语...
    空谷悠阅读 2,925评论 4 97
  • 一、数组 1、数组定义 数组就是一个键值对组成的语言结构,键类似于酒店的房间号,值类似于酒店房间里存储的东西。 $...
    空谷悠阅读 695评论 4 12
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 5,716评论 0 17
  • 一、会话控制(session与cookie) 1.cookie简介 Cookie是存储在客户端浏览器中的数据,我们...
    空谷悠阅读 509评论 0 5
  • PHP 算数运算符 ";echo ($a-$b)." ";echo ($a*$b)." ";echo ($a/$b...
    久久归移阅读 359评论 0 0