Googleナレッジグラフ検索API Linkを使用してGoogleから画像を取得しようとしています。jsonファイルを使用してjsonファイル内の画像のsrcが空であることを確認します。
{
"@context": {
"@vocab": "http://schema.org/",
"goog": "http://schema.googleapis.com/",
"resultScore": "goog:resultScore",
"detailedDescription": "goog:detailedDescription",
"EntitySearchResult": "goog:EntitySearchResult",
"kg": "http://g.co/kg"
},
"@type": "ItemList",
"itemListElement": [
{
"@type": "EntitySearchResult",
"result": {
"@id": "kg:/m/0dl567",
"name": "Taylor Swift",
"@type": [
"Thing",
"Person"
],
"description": "Singer-songwriter",
"image": {
"contentUrl": "https://t1.gstatic.com/images?q=tbn:ANd9GcQmVDAhjhWnN2OWys2ZMO3PGAhupp5tN2LwF_BJmiHgi19hf8Ku",
"url": "https://en.wikipedia.org/wiki/Taylor_Swift",
"license": "http://creativecommons.org/licenses/by-sa/2.0"
},
"detailedDescription": {
"articleBody": "Taylor Alison Swift is an American singer-songwriter and actress. Raised in Wyomissing, Pennsylvania, she moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music. ",
"url": "http://en.wikipedia.org/wiki/Taylor_Swift",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
},
"url": "http://taylorswift.com/"
},
"resultScore": 896.576599
}
]
}
この目的のためにJavaScriptコードの主要部分は以下の通りである:
<script>
var service_url = 'https://kgsearch.googleapis.com/v1/entities:search';
var params = {
'query': 'Taylor Swift',
'limit': 10,
'indent': true,
'key' : 'AIzaSyBpCW-EUz2EqI8YIjmQYYXwTzZu8kXGPEw',
};
var img;
$.getJSON(service_url + '?callback=?', params, function(response) {
$.each(response.itemListElement, function(i, element) {
img = $('<img>', {
src: element['result']['image']['contentUrl']
});
if ($(img).attr('src') !== '') {
img.appendTo(document.body);
}
else {
$('body').html('<p>image is not available</p>');
}
});
});
</script>
あなたはjavascriptのコードから見ることができるように、私はGoogleからの返されたJSONファイルは、以下のこの1に似ていますGoogleナレッジグラフから10枚の画像を取得しようとしています。ただし、Googleナレッジグラフですべての画像を使用できるわけではありません。つまり、一部のURLが空である可能性があります。私は、画像のURLが空でないかどうかをチェックしたい、画像はdocument.bodyに追加されます。そうでないと、のようなエラーメッセージは利用できません。が追加されます。問題は、以下の文が私にとってうまくいかないことです。
私は最初の画像しか得ておらず、残りの部分をチェックするためにループが停止しました。誰かが私のための方向を指すことができますか?私は運がなければ、たくさんのグーグルを探せました。ありがとうございます。
代わりに、なぜあなたはcontentUrl値をチェックしていけないのimg SRCをチェックするの? –