Unable to get results more than 100 results on google custom search api

て烟熏妆下的殇ゞ 提交于 2020-05-25 17:12:49

问题


I need to use Google Custom Search API https://developers.google.com/custom-search/v1/overview. From that page, it said:

For CSE users, the API provides 100 search queries per day for free. If you need more, you may sign up for billing in the Developers Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day.

I already sign up for billing inside the developer console. However, I still could not retrieve results more than 100. What things should I do more? https://www.googleapis.com/customsearch/v1?cx=CSE_INSTANCE&key=API_KEY&q=QUERY&start=100

{ error: { errors: [ { domain: "global", reason: "invalid", message: "Invalid Value" } ], code: 400, message: "Invalid Value" } }


回答1:


Query: Definition
https://support.google.com/customsearch/answer/1361951

Any actual user query from a Google Site Search engine, including but not limited to search engines installed on your website using XML, iFrame, or the Custom Search Element.

That means you would probably need to send eleven queries to get more than 100 results.

GET https://www.googleapis.com/customsearch/v1?&q=QUERY&...&start=1
GET https://www.googleapis.com/customsearch/v1?&q=QUERY&...&start=11
GET https://www.googleapis.com/customsearch/v1?&q=QUERY&...&start=21
GET ...
GET https://www.googleapis.com/customsearch/v1?&q=QUERY&...&start=81
GET https://www.googleapis.com/customsearch/v1?&q=QUERY&...&start=91 
GET https://www.googleapis.com/customsearch/v1?&q=QUERY&...&start=101

Check every response and if error code is 400, you can stop - there is probably no need to send next (&start=previous+10) request.

Now you can merge responses and start building results page.

Google Custom Search and Google Site Search return up to 10 results per query. If you want to display more than 10 results to the user, you can issue multiple requests (using the start=0, start=11 ... parameters) and display the results on a single page. In this case, Google will consider each request as a separate query, and if you are using Google Site Search, each query will count towards your limit.

There might be a better way to do this then I described above. (But, I'm not sure about batching API calls.)

And (finally) possible answer to your question: I made more than few tests, but I haven't had any luck with start greater than 100 (I was getting the same as you - <Response [400]>). I'm using "Browser key" from my billing-enabled project. That could mean we can't get 101st, 102nd, 103rd, etc. results with CSE API.



来源:https://stackoverflow.com/questions/35633466/unable-to-get-results-more-than-100-results-on-google-custom-search-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!