Closed yuta0801 closed 7 years ago
調べてみましたが、Google画像検索結果のHTMLソース上では画像タグが以下の様になっていました。
<img class="rg_ic rg_i" data-sz="f" name="ZcvDYlgt8bMONM:" alt="「node.js」の画像検索結果" jsaction="load:str.tbn" onload="google.aft&&google.aft(this)">
HTMLソースの時点ではimg
タグにsrc
属性が指定されていません。
おそらくonload
のタイミングでsrc
にbase64化された画像が差し込まれるのではないかと思います。
cheerio-httpcliではonload
といった動的なコンテンツ変更には対応していないので、src
が指定されていないimg
タグのsrc
を見に行ってエラーになっているといった状態です。
次バージョンで修正する予定ですが、現在masterブランチで大きめの対応を行っており、それと合わせてリリースする関係上、すぐにバージョンアップというわけにはいかない状態なので、急ぎの場合は、エラーの出た箇所を以下のように書き換えるととりあえず動くようにはなります。
var b64chk = ($elem.attr('src') || '').match(/^data:(image\/\w+);base64,([\s\S]+)$/i);
ただ、Googleの画像検索結果が上記のように動的にimg
のsrc
をセットしていることから考えると、この修正を適用しても検索結果の画像を正常に全部取得できるかどうかは分かりません。
静的コンテンツの解析がcheerio-httpcliの機能なので、その点についてはご了承ください。
こちらは0.7.0で修正されました。
画像をダウンロードするサンプルをGoogleの画像検索の結果の画像を ダウンロードさせるようにしたら以下のようなエラーがでました。
試しにdownload.js:312の$elem.attr('src')の値を見てみるとundefinedでした。