Custom Search Engines

Source: Internet
Author: User

Google Custom Search engine

This article focuses on how to define your own search engine through Google's API and embed the Google search box in your Web page. In addition, the URL of the custom search engine request data is analyzed, the request is simulated and the result of the search is obtained.

1 write in front

Some time ago found a Web site to provide search Baidu network resources, very easy to use, but a lot of ads, so they did a WinForm form program to get the site's data requests, parse and bind the results of the request. Finally, the realization of the WinForm program of this process to share the blog park, that is, my last blog post "Baidu Network Disk resource finder."

I found that the Web site that provided the search for the grid resources was built using the Google search engine API. Search for some information on the Internet, the function of Google Custom search engine, found that it is really fun. Then there are colleagues asked if you can introduce how to implement the use of Google search API, then let's start.

2 Custom Search Engines

1) Customize Google's search engine, that first has to have a Google account Ah, this is also the first step, first register a Google account.

2) After registering for Google's account, go to the Custom Search engine page, https://cse.google.com/cse/, page below.

3) Click on the "New search engine" on the right and fill in the basic configuration of your search engine. The page below.

As I can see, you just need to configure three items to complete a custom search engine.

    • The first is the site you need to search for, meaning that the search engine you customize will only search for the information you want in a single page, the entire site, or the entire domain. Here we take Baidu network disk resources for example, fill in www.pan.baidu.com .
    • The second one is language, which we choose all languages.
    • The last one is the search engine name, we will fill out "Baidu Cloud disk Search"

4) Click Create, the implementation of their own search engine, click on the right side of the "Modify Search engine" page, such as.

In the Revision Search engine page has a "search engine ID", this is particularly important, you can click on it, see your search engine ID code, you can also open your search engine URL. Another important thing is "get the code", get this code can be implemented to embed the custom search engine box into your own definition of the Web page, click "Get Code". Able to get to a piece of HTML code, as follows:

Also, in the 1 above, step 4), you can test your custom search engine. Here, we enter the "math" keyword, and the results are as follows.

3 How to use a custom search engine

In the second section we have created our own search engine, so how do we use it? Here, I take two ways, the first way to directly copy the code in the second section and embed it in my Web page, the second way is to analyze the URL structure and request method of the custom search engine request, then constructs the search URL, simulates the request to obtain the query result.

3.1 Embedding Web pages

Start by customizing an HTML page, and then copy the code described in the second section into the Div, as shown in the code below.

<Html><Body><Meta charset="Utf-8"><Style Type="Text/css"> #main{ Width750px; Margin20px Auto; } #main P{ Text-align:Center; Font-size:25px; Font-weight:Bold; Margin-button:100px; } </Style></Body><Head><Div Id="Main"><p> Baidu Net disk resource Finder @jaume</P><Script>(function(){ VarCx= ' Search engine ID '; VarGcse= Document.CreateElement(' Script '); Gcse.Type = ' Text/javascript '; Gcse.Async = True; Gcse.Src = ' Https://cse.google.com/cse.js?cx= ' +Cx; VarS= Document.getElementsByTagName(' Script ')[0]; S.ParentNode.InsertBefore(GCSE,S; })()</< Span class= "Hljs-name" >script><GCSE: Search></gcse:< Span class= "Hljs-tag" >search> </div> </head >< /HTML>           

Note: Above var cx=‘搜索引擎ID , this "search engine ID" is generated automatically when creating a custom search engine in section two, replacing the search engine ID you created with this one. Here is the page effect.

This allows you to embed a custom search engine into your own web page.

3.2 Impersonation Request

Through analysis, search engine search request URL is:

https://www.googleapis.com/customsearch/v1element?key=AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY&rsz=filtered_cse&num=10&hl=zh_CN&prettyPrint=false&source=gcsc&gss=.com&sig=0c3990ce7a056ed50667fe0c3873c9b6&cx=搜索引擎ID&q=数学&sort=&googlehost=www.google.com

URL Main parameter analysis:

    • CX: Search engine ID
    • Q: Search Key
    • Sort: How search results are sorted

Construct the URL of the above pattern, use get to request, will get the JSON data returned as follows, the JSON data contains the search results and the accompanying attributes (total number of search results, etc.). As shown in.

Understanding this process of engine request search results, it is very simple to implement in C # language. The code is so simple that it is not posted.

4 Summary

One of the most critical points to using Google's API to customize a search engine is that your machine needs to be able to access Google. If you can't access Google, you can only/(ㄒoㄒ)/~~. This example is to search Baidu network disk resources, of course, you can also search all the resources of the blog park.

In fact, Google custom search engine API has more exciting settings, you can go to play.

Custom Search Engines

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.