PHP+phpQuery備份Pixiv的bookmark(お気に入りユーザー)

最近老實說又多玩了一個語言,外加我今年算是個考生,所以說很多時候晚上忙一下就已經11點多了,當然最近都在養肝,忙完就睡著了...XD

至於廢話就不多說了,最近學了一點Action Script 3.0,但是我基本上還是用Flash CS4下去學習,改天會開始去摸摸看Flex,希望是有時間就好(笑)。但是老實說在這個7月時也算是我過的最充實的一個月了,因為搞懂了非常之多的東西。

這一個PHP只能算是一個小部份,因為也是一個小程式....XD,但是基本上需要phpQuery,因為這個函式庫對於要抓取網頁資訊來說的話非常之方便而且簡單明瞭,對於一個複雜的網頁原始碼而言,可以簡化取出需要的資訊之後,再去做preg的動作繪比較的簡單。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
< ?php
    require 'phpQuery/phpQuery/phpQuery.php';

    $loginurl='http://www.pixiv.net/index.php';
    $pixiv_id='你的PIXIV_id';
    $password='password';
    //以上為一些登入基本資訊

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5); InfoPath.2)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_COOKIESESSION,true);
    curl_setopt($curl, CURLOPT_URL, $loginurl);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'mode=login&pixiv_id='.$pixiv_id.'&pass='.$password );
    curl_exec($curl);
    //登入的動作

    $p=1;//設定起始頁
    $bookmark_rest='show';//hide為非公開 show為公開

    do{
        $url='http://www.pixiv.net/bookmark.php?type=user&rest='.$bookmark_rest.'&p='.$p; //bookmark網址
        curl_setopt($curl, CURLOPT_URL, $url);
        $profile=curl_exec($curl);

        phpQuery::newDocument($profile)->find('html > body > div#wrapper > div#pixiv > div#content2');
        $title = pq('div[align="right"] > a') -> html();
        preg_match('/次の(.*?)件/',$title,$match);
        //是否有下一頁的判斷用

        $title2 = pq('form#f > div[style="width:140px;height:120px;float:left;text-align:center;"]') -> html();
        $title2=preg_replace('/\n/', '',$title2);
        $title2=preg_replace('/<input (.*?)/>/', '',$title2);
        $title2=preg_replace('/<img (.*?)/>/', '',$title2);
        preg_match_all('/<a href="member.php\?id=(.*?)">< \/a><div style=\"padding-top:5px;\">(.*?)< \/div>/', $title2,$match2,PREG_SET_ORDER);

        foreach ($match2 as $val) {
            echo 'http://www.pixiv.net/member.php?id='.$val[1].' - '.$val[2]."\n";
        }//映出所有的繪師
        $p++;
    }while(!empty($match));

    curl_close($curl);
?>

以上就是一個非常簡單的小範例。對於熟悉PHPQuery之後要處理抓取網頁的資訊的動作的話真的非常的簡單。

這是一個非常簡單的,把PIXIV上的喜愛繪師的網址給備份出來的一個小程式,用這個列出來後才知道,原來我已經收了快要1150個繪師了...Orz

Thu Aug. 6 2009
Comments

Comments