iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 9
0
自我挑戰組

用 laravel 尻出自己形狀的 line bot,還要撐三十天!系列 第 9

【第九天】第一次爬漫畫就上手

【第九天】第一次爬漫畫就上手

嗨~各位晚安!昨天已經完成了推送巴哈當天最新動話的實作,趁現在手還熱的,我們接著來試試看漫畫吧!

就從99770下手!

進去之後隨便看個海賊王好了,會發現有個170很常出現在他的內容裡面,也是網址的部分之一,就大膽猜測這就是他的某種id!
https://ithelp.ithome.com.tw/upload/images/20171214/201073804UGIiYz8ob.png
https://ithelp.ithome.com.tw/upload/images/20171214/201073806O24iXHdUX.png
再去搜尋個東京湌種:RE,來驗證看看25010
https://ithelp.ithome.com.tw/upload/images/20171214/201073808Z7kG0LEt0.png

BINGO!

好!那我們就開始寫code吧!大家60分鐘後見~!

DONE!

CrawlerService.php

    public function getNewEpisodeFromComic99770(Crawler $crawler): array
    {
        $imagePath = $crawler->filterXPath('//div[@class="cDefaultImg"]/img')->attr('src');
        $directUri = $crawler->filterXPath('//table[@class="cInfoTxt"]/tr[5]/td[2]/a')->attr('href');
        $label = $crawler->filterXPath('//table[@class="cInfoTxt"]/tr[5]/td[2]/a')->text();
        $date = $crawler->evaluate('substring-after(//table[@class="cInfoTxt"]/tr[5]/td[2], ":")');

        return [
            'date' => array_first($date),
            'directUri' => $directUri,
            'imagePath' => $imagePath,
            'label' => $this->getFilterString($label),
        ];
    }

原本說好六十分鐘,結果花了快兩小時!殘念!!
順便分享一下中途採的雷!

  • 一個是過濾掉我們不要的HTML相關字元 參考
  • 一個是LINE的推送一直發不出去
{"message":"A message (messages[0]) in the request body is invalid","details":[{"message":"invalid uri scheme","property":"template[@type=image_carousel].columns[0].imageUrl

後來發現原因是在於LINE的縮圖只吃https,不吃http
恩~雖然是很久的考古題了!但我們明天就稍為輕鬆一下認識這兩者之間的差異吧!

所以今天的成果大概是這樣~明天見啦!
https://ithelp.ithome.com.tw/upload/images/20171214/20107380TngflIiveK.png


上一篇
【第八天】完成當季動畫通知推送!
下一篇
【第十天】支線任務:單數錯了嗎?
系列文
用 laravel 尻出自己形狀的 line bot,還要撐三十天!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言