querylist爬取頁面內容rules記錄以及爬蟲字符編碼的問題


  1. querylist真的挺好用的!!!感謝!!!參考鏈接:https://learnku.com/laravel/t/6262/querylist-4-concise-and-elegant-php-collection-tool 
  2. 文檔v4:http://querylist.cc/docs/guide/v4/example
  3. 記錄幾個rules,僅方便自己查看
    $rules = [
        'p1' => ['#side > div.content.clearfix > div.content_left > div.centent_centent > p:nth-child(1)', 'text'],  //文本,比如圖片就會被過濾掉
        'content' => ['#side > div.content.clearfix > div.content_left > div.centent_centent', 'html'],  //HTML 包含標簽等,圖片會爬下來,類似富文本
        'img' => ['#side > div.content.clearfix > div.content_left > div.centent_centent > p:nth-child(4) > img', 'src'],  //圖片的鏈接
         'alt' => ['#side > div.content.clearfix > div.content_left > div.centent_centent > p:nth-child(4) > img', 'alt'],  //img 的 alt
    'a' => ['h3>a', 'href']; //a 標簽的href 鏈接
    ];

     

  4. 簡單記錄一下在爬取數據保存數據的過程中遇到的頭疼的問題,問題是:爬取到的數據是亂碼的(數據content是富文本,包含標簽的),然后我想着使用header('Content-Type: *; charset=utf-8');  設置字符集,數據是爬到了,但是保存到數據庫卻是空的(成功保存了,但內容為空)。不知道是什么原因,是因為字符編碼的原因導致mysql不能 保存數據 還是因為別的原因,我到現在也還沒找到根源。(自己太菜了-_-#)。我一步一步排錯,(1)打印數據是有內容的(2)打印SQL語句在命令行執行,也成功了(數據庫保存了正常的數據)。(3)PDO,mysqli都嘗試了(都成功地向數據庫插入了空值)。(4)我還一度嘗試SQL語句的單引號、雙引號更改,然並卵----------我也是醉了。。。。。后來看到iconv,又嘗試iconv (沒辦法的時候,就是不斷的嘗試各種可能)。發現成功了。雖然成功了,但是不知道什么原因 #_-  |  -_#  猜測是因為mysql有着字符編碼的限制吧。反正爬蟲的時候不要用header 設置字符編碼,應該使用iconv 來轉換字符編碼。就能夠正常保存到數據庫。
     1 <?php
     2 
     3 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
     4 // 駕照資訊內容爬取url:http://www.jsyst.cn/ksjq/km1/index.asp?page=7 //
     5 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
     6 
     7 require '../querylist_spider/vendor/autoload.php';
     8 
     9 
    10 use QL\QueryList;
    11 
    12 //header('Content-Type: *; charset=gb2312');  //我開啟這個之后,爬取的數據無法保存到數據庫,找了一天的bug,沒找到問題,第二天換了一個轉碼方式,iconv() 成功保存了。心里真的有一萬只草擬馬,同時感到自己真的是太菜了,#-_-#
    13 
    14 // $page = [1, 2, 3, 4, 5, 6, 7, 8];
    15 $page = [1, 2];
    16 // 獲取a標簽的內容以及url
    17 foreach ($page as $key => $value) {
    18     $aUrl[] = 'http://www.jsyst.cn/ksjq/km1/index.asp?page='.$value;
    19 }
    20 
    21 for ($i = 1; $i <= 10; $i++) {
    22     $rule[$i]['title'] = ["#side > div.content.clearfix > div.content_left > div > p:nth-child({$i}) > a", 'text'];
    23     $rule[$i]['url'] = ["#side > div.content.clearfix > div.content_left > div > p:nth-child({$i}) > a", 'href'];  // 要爬取的詳情頁的url
    24 }
    25 
    26 foreach ($aUrl as $key => $value) {
    27     foreach ($rule as $k => $v) {
    28         $list[] = QueryList::rules($v)->get($value)->query()->getData()->all();
    29     }
    30 }
    31 
    32 // print_r($list); exit('list exit code');
    33 
    34 $rules = [
    35     //'p1' => ['#side > div.content.clearfix > div.content_left > div.centent_centent > p:nth-child(1)', 'text'],
    36     'content' => ['#side > div.content.clearfix > div.content_left > div.centent_centent', 'html'],
    37     //'img' => ['#side > div.content.clearfix > div.content_left > div.centent_centent > p:nth-child(16) > img', 'alt'],
    38 ];
    39 
    40 foreach ($list as $key => $value) {
    41     $data[$key]['content'] = QueryList::rules($rules)->get($value[0]['url'])->query()->getData()->all()[0]['content'];
    42     $data[$key]['title'] = $value[0]['title'];
    43     if ($key == 5) {
    44         break;
    45     }
    46 }
    47 
    48 // file_put_contents('./data.txt', var_export($data, true));
    49 // print_r($data);
    50 // exit;
    51 
    52 // 插入數據庫
    53 
    54 $link = mysqli_connect('localhost', 'root', 'root', 'count') or exit('鏈接數據庫失敗');
    55 //$link->query('set charset=utf8');
    56 
    57 $addtime = $updatetime = time();
    58 foreach ($data as $key => $value) {
    59     $title = iconv('gb2312', 'utf-8', $value['title']);
    60     $content = iconv('gb2312', 'utf-8', $value['content']);
    61     
    62     $sql = 'INSERT INTO drive_cheats (title, content, addtime, updatetime) VALUES (\''.$title.'\', \''.$content.'\', \''.$addtime.'\', \''.$updatetime.'\'); ';
    63     // echo $sql;exit;
    64     $res = mysqli_query($link, $sql);
    65     if (!$res) {
    66         echo mysqli_error($link);
    67     } else {
    68         // echo '<font color="red">success</font>';
    69         // echo mysqli_info($link);
    70         echo mysqli_insert_id($link);
    71     }
    72     echo $sql;
    73 }
    74 
    75 mysqli_close($link);
    View Code

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM