如何在不将表单发送给Google本身的情况下在同一页面上显示google结果

问题描述 投票:0回答:1

<!DOCTYPE html>
<head>
</head>
<body>
<form action="https://www.google.com/search" method="GET">
<input type="text" name="q" placeholder="MySearch">
<input type="submit" value="MySearch">
</form>
</body>
</html>

这会将页面发送给Google。我想停留在我的页面上,只显示来自Google的列表结果。

jquery html search
1个回答
0
投票

可以使用Google Search API,您可以在下面找到代码段,并在https://developers.google.com/custom-search/docs/tutorial/introduction上详细了解其工作方式。

祝你好运

function getGoogleSearchResults(q) {

// Get the API key from Google's developer console
// Get the CSE ID from google.com/cse

var api = "https://www.googleapis.com/customsearch/v1?key=" +
    KEY + "&cx=" + CSE + "&q=" + encodeURIComponent(q);

try {

    var response = UrlFetchApp.fetch(api, {
        muteHttpExceptions: true
    });

    if (response.getResponseCode() == 200) {

        var content = JSON.parse(response);

        // Did the search return any results?
        if (content.searchInformation.totalResults > 0) {

            var count = content.items.length;

            for (var i = 0; i < count; i++) {

                // Save the page title, description and hyperlink
                Logger.log(content.items[i].title);
                Logger.log(content.items[i].snippet);
                Logger.log(content.items[i].link);
            }
        }
    }
} catch (f) {
    Logger.log(f.toString());
}

}

© www.soinside.com 2019 - 2024. All rights reserved.