如何通过node.js使用自定义搜索网站受限JSON API?

时间:2019-03-22 06:26:05

标签: javascript node.js google-custom-search

我使用bing搜索引擎Api来获取给定公司的员工人数。我制作了一个函数,其中我从data.Csv文件中提取公司的网址,然后使用BingBing Api上进行搜索,并从该受雇者那里获得雇​​员总数,然后将每个结果写入我的csv中out.csv。它工作得很好,但现在我想对谷歌做同样的事情。这是我使用bing api的代码

“严格使用”;

const csvReader = require('csv-parser');
const csvW = require('csv-writer');
const fs = require('fs');
var request = require('sync-request');


// Replace the subscriptionKey string value with your valid subscription 
key.
let subscriptionKey = '6804bc5f804e47edaa4d8466928bd8de';
let host = 'https://api.cognitive.microsoft.com';
let path = '/bing/v7.0/search';

let outputData = [];
fs.createReadStream('data.csv')
.pipe(csvReader())
.on('data', (row) => {
// console.log(row.url);
bing_web_search(row.url);
})
.on('end', () => {
 console.log('CSV file successfully processed');
 csvWriter
  .writeRecords(outputData)
  .then(() => console.log('The CSV file was written successfully', 
 outputData));

 });

let bing_web_search = function (url) {
let search = url + ' see all employees on LinkedIn ->';
console.log('Searching the Web for: ' + search);

let body = '';
let snippet = '';
let fullPath = host + path + '?q=' + encodeURIComponent(search);
var res = request('GET', fullPath, {
headers: {
  'Ocp-Apim-Subscription-Key': subscriptionKey,
},
});
body += res.getBody('utf8');
let v = JSON.parse(body);
if (v && v.webPages && v.webPages.value.length > 0) {
snippet = v.webPages.value[0].snippet;
console.log(v.webPages.value);
}

const r = new RegExp(`(0|[1-9][0-9]{0,2}(?:(,[0-9]{3})*|[0-9]*))(\.[0-9]+) 
{0,1}`);
const results = r.exec(snippet);
let employees = 0;
if (results && results.length > 0) {
 employees = results[0];
} else {
employees = "NOT_FOUND";
}
console.log('employees =', employees);

const data = { 'url': url, 'employees': employees };
outputData.push(data);
}

const createCsvWriter = csvW.createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: 'out.csv',
header: [
{ id: 'url', title: 'Url' },
{ id: 'employees', title: 'Employees' },
]
});

这是我的data.csv格式

https://www.linkedin.com/company/130323
https://www.linkedin.com/company/3686999
https://www.linkedin.com/company/1384

但是现在我正在尝试对Custom Search Site Restricted JSON API做同样的事情,这是我尝试过的代码

“严格使用”;

const csvReader = require('csv-parser');
const csvW = require('csv-writer');
const fs = require('fs');
var request = require('sync-request');


let subscriptionKey = 'My-Key';

let cx = 'My-Cx-Key';

let path = `https://www.googleapis.com/customsearch/v1/siterestrict? 
key=${subscriptionKey}&cx=${cx}&q=`;



let outputData = [];
fs.createReadStream('data.csv')
.pipe(csvReader())
.on('data', (row) => {
bing_web_search(row.url);
})
.on('end', () => {
console.log('CSV file successfully processed');
csvWriter
  .writeRecords(outputData)
  .then(() => console.log('The CSV file was written successfully', 
outputData));

});

 let bing_web_search = function (url) {
 let search = url + ' see all employees on LinkedIn ->';
 console.log('Searching the Web for: ' + search);

 let body = '';
 let snippet = '';

 let fullPath = path + encodeURIComponent(search);
 var res = request('GET', fullPath);
 body += res.getBody('utf8');
 let v = JSON.parse(body);
 console.log(v);

  }

但是它给我这样的错误

{
 "error": {
 "errors": [
   {
    "domain": "usageLimits",
    "reason": "keyInvalid",
    "message": "Bad Request"
   }
  ],
"code": 400,
"message": "Bad Request"
}

}

我该如何解决,我不知道这是什么问题?

注意:出于安全考虑,我将所有秘密密钥替换为要在此处发布的字符串

0 个答案:

没有答案