Kev*_*one 5 google-docs google-apps-script google-custom-search
我一直在尝试以下代码
var response = UrlFetchApp.fetch("https://www.google.com/#q=this+is+a+test");
var contentText = response.getContentText();
Logger.log(contentText);
var thisdoc=DocumentApp.getActiveDocument().getBody() ;
thisdoc.setText(contentText);
Logger.log(contentText.indexOf("About"));
Run Code Online (Sandbox Code Playgroud)
但它似乎只返回标题,空体,没有搜索结果.至少我应该能够在浏览器的顶部看到"关于xxx结果",但这不会出现在文本中,indexOf也不会返回正屏幕.我想知道搜索结果是否填充后页面加载意味着正文标记确实是空的,如果有,是否有解决方法?
编辑:不,它不会破坏TOS,因为这是一个GAFE应用程序(这是一个商业应用程序),对于企业帐户,他们有免费和高级模式访问他们的API.
Mog*_*dad 11
Google为授权搜索提供了API,因此不要轻易抓取网页.
例如,你可以使用自定义搜索API使用UrlFetch().
从脚本编辑器转到Resources -> Developer's Console Project... -> View Developer's Console.为公共API访问创建新密钥.按照自定义搜索API文档中的说明创建自定义搜索引擎.在指示的脚本中输入密钥和ID.(更多细节如下.)
此示例脚本将返回包含成功搜索结果的对象; 您可以导航对象以提取您想要的任何信息.
/**
* Use Google's customsearch API to perform a search query.
* See https://developers.google.com/custom-search/json-api/v1/using_rest.
*
* @param {string} query Search query to perform, e.g. "test"
*
* returns {object} See response data structure at
* https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response
*/
function searchFor( query ) {
// Base URL to access customsearch
var urlTemplate = "https://www.googleapis.com/customsearch/v1?key=%KEY%&cx=%CX%&q=%Q%";
// Script-specific credentials & search engine
var ApiKey = "--get from developer's console--";
var searchEngineID = "--get from developer's console--";
// Build custom url
var url = urlTemplate
.replace("%KEY%", encodeURIComponent(ApiKey))
.replace("%CX%", encodeURIComponent(searchEngineID))
.replace("%Q%", encodeURIComponent(query));
var params = {
muteHttpExceptions: true
};
// Perform search
Logger.log( UrlFetchApp.getRequest(url, params) ); // Log query to be sent
var response = UrlFetchApp.fetch(url, params);
var respCode = response.getResponseCode();
if (respCode !== 200) {
throw new Error ("Error " +respCode + " " + response.getContentText());
}
else {
// Successful search, log & return results
var result = JSON.parse(response.getContentText());
Logger.log( "Obtained %s search results in %s seconds.",
result.searchInformation.formattedTotalResults,
result.searchInformation.formattedSearchTime);
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
例:
[15-05-04 18:26:35:958 EDT] {
"headers": {
"X-Forwarded-For": "216.191.234.70"
},
"useIntranet": false,
"followRedirects": true,
"payload": "",
"method": "get",
"contentType": "application/x-www-form-urlencoded",
"validateHttpsCertificates": true,
"url": "https://www.googleapis.com/customsearch/v1?key=--redacted--&cx=--redacted--&q=test"
}
[15-05-04 18:26:36:812 EDT] Obtained 132,000,000 search results in 0.74 seconds.
Run Code Online (Sandbox Code Playgroud)
(摘自Google的文档.)
选择一个项目,或创建一个新项目.

在左侧的侧栏中,展开API和auth.接下来,单击API.在API列表中,确保Custom Search API的状态为ON .

...

在左侧的边栏中,选择凭据.
通过单击" 公共API访问"下的" 创建新密钥"来创建应用程序的API密钥.要使用Google Script,请创建一个浏览器密钥.
一旦对浏览器应用的关键是创建,复制API密钥到您的代码.
按照此处的说明操作.创建自定义搜索引擎后,将搜索引擎ID复制到代码中.
| 归档时间: |
|
| 查看次数: |
7017 次 |
| 最近记录: |