Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created June 5, 2014 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/48e875887bdeb7df5905 to your computer and use it in GitHub Desktop.
Save dongyuwei/48e875887bdeb7df5905 to your computer and use it in GitHub Desktop.
import urllib
URI = "http://google.com/complete/search?output=firefox&hl=en&q="
def suggest(term):
text = urllib.urlopen(URI + urllib.quote(term)).read()
arr = eval(text)
return arr[1]
print suggest("python")
@dongyuwei
Copy link
Author

@dongyuwei
Copy link
Author

@dongyuwei
Copy link
Author

#import <Foundation/Foundation.h>
//... see http://www.flaviodiez.com/2013/08/json-with-objective-c/
void testGoogleSuggestion(){
    NSURL * url = [[NSURL alloc] initWithString:@"http://google.com/complete/search?output=firefox&hl=en&q=halle"];

    // Prepare the request object
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                            timeoutInterval:30];


    NSURLResponse *response;
    NSError *error;

    NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest
                                    returningResponse:&response
                                                error:&error];



    NSArray* object = [NSJSONSerialization
                       JSONObjectWithData:data
                       options:0
                       error:&error];
    if(!error){
        NSLog(@"%@",object[1]);

    }else{
        NSLog(@"error: %@",error);
    }
}

@dongyuwei
Copy link
Author

wiktionary api need a custom User-Agent header

@dongyuwei
Copy link
Author

http://nssug.baidu.com/su?x=1&prod=recon_dict&wd=halle&p=3&t=1402911230200 百度词典的速度非常快,20~30毫秒,对输入法而言在可接受范围内

@dongyuwei
Copy link
Author

@dongyuwei
Copy link
Author

call google suggestion jsonp api

$.jsonp({
    url: 'http://google.com/complete/search?output=firefox&hl=en&q=halle',
    callbackParameter: "callback",
    success: function(data) {
        console.log(data);
    },
    error: function() {
        console.log(arguments);
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment