Last active
December 29, 2015 05:49
-
-
Save rabintang/7624134 to your computer and use it in GitHub Desktop.
利用Google提供的API来对输入的词做拼写检查,并得到来自Google返回的建议
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
import os | |
import sys | |
import urllib2,urllib | |
import HTMLParser | |
import xml.dom.minidom | |
import xml.etree.ElementTree as ET | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
#利用Google提供的API来做拼写检查 | |
class getGoogleSuggestion: | |
def __init__(self): | |
self.cx = 'XXXXXXXXXXXXXXXXXXXXXXXX' | |
def getSuggestion(self,query): | |
url = ('http://www.google.com/search?' | |
'q=%s' | |
'&hl=zh' | |
'&output=xml' | |
'&client=google-csbe' | |
'&cx=%s')%(urllib.quote(query),self.cx) | |
request = urllib2.Request(url, None) | |
response = urllib2.urlopen(request) | |
text = response.read() | |
tag = '<Suggestion q="' | |
result = '' | |
if text.find(tag)!=-1: | |
h= HTMLParser.HTMLParser() | |
result_xml = h.unescape(text) | |
pos = result_xml.find(tag)+len(tag) | |
end = result_xml.find('"',pos) | |
result = result_xml[pos:end] | |
return result | |
if __name__=='__main__': | |
test = getGoogleSuggestion() | |
keyword = '丁军辉' | |
#keyword = 'socer' | |
#keyword = '丁俊晖' | |
test.getSuggestion(keyword) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment