【发布时间】:2015-10-30 08:00:59
【问题描述】:
好的,所以我想使用 google api 进行图像搜索,我知道不推荐使用,但我不想使用自定义搜索
这是我一直在使用的代码
<script type="text/javascript">
google.load('search', '1');
var imageSearch;
function addPaginationLinks() {
// To paginate search results, use the cursor function.
var cursor = imageSearch.cursor;
var curPage = cursor.currentPageIndex; // check what page the app is on
var pagesDiv = document.createElement('div');
for (var i = 0; i < cursor.pages.length; i++) {
var page = cursor.pages[i];
if (curPage == i) {
// If we are on the current page, then don't make a link.
var label = document.createTextNode(' ' + page.label + ' ');
pagesDiv.appendChild(label);
} else {
// Create links to other pages using gotoPage() on the searcher.
var link = document.createElement('a');
link.href="/image-search/v1/javascript:imageSearch.gotoPage("+i+');';
link.innerHTML = page.label;
link.style.marginRight = '2px';
pagesDiv.appendChild(link);
}
}
var contentDiv = document.getElementById('content');
contentDiv.appendChild(pagesDiv);
}
function searchComplete() {
// Check that we got results
if (imageSearch.results && imageSearch.results.length > 0) {
// Grab our content div, clear it.
var contentDiv = document.getElementById('content');
contentDiv.innerHTML = '';
// Loop through our results, printing them to the page.
var results = imageSearch.results;
for (var i = 0; i < results.length; i++) {
// For each result write it's title and image to the screen
var result = results[i];
var imgContainer = document.createElement('div');
var title = document.createElement('div');
// We use titleNoFormatting so that no HTML tags are left in the
// title
title.innerHTML = result.titleNoFormatting;
var newImg = document.createElement('img');
// There is also a result.url property which has the escaped version
newImg.src="/image-search/v1/result.tbUrl;"
imgContainer.appendChild(title);
imgContainer.appendChild(newImg);
// Put our title + image in the content
contentDiv.appendChild(imgContainer);
}
// Now add links to additional pages of search results.
addPaginationLinks(imageSearch);
}
}
function OnLoad() {
// Create an Image Search instance.
imageSearch = new google.search.ImageSearch();
// Set searchComplete as the callback function when a search is
// complete. The imageSearch object will have results in it.
imageSearch.setSearchCompleteCallback(this, searchComplete, null);
// Find me a beautiful car.
imageSearch.execute("Ferrari");
// Include the required Google branding
google.search.Search.getBranding('branding');
}
google.setOnLoadCallback(OnLoad);
</script>
问题是当我运行它时,图像没有出现
我知道问题是它使用的网址不起作用
问题出在这行代码
newImg.src="/image-search/v1/result.tbUrl;"
但我不知道是否有其他网址可以使用或我能做什么
我不能使用 JQuery,这是一个大学项目,我们不能使用 JQuery
感谢您的关注!
【问题讨论】:
-
你使用的是他们给你的id吗?我们复制和粘贴的一些代码有一个需要替换的通用 id。
-
我也有同样的问题。没有显示结果。当我显示日志时,
imageSearch.results中没有任何结果 -
GimageSearch 上的 XHR 调用返回
callback google.search.ImageSearch.RawCompletion('1', null, 403, 'This API is no longer available.', 200)我认为我们不能使用这个 API
标签: javascript google-api google-image-search