Last active
January 26, 2022 16:02
-
-
Save fpt/b21b8abea239392129b6 to your computer and use it in GitHub Desktop.
Google Suggest API example
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
#!/usr/bin/env python | |
# coding:utf-8 | |
import pprint | |
import requests | |
from lxml import etree | |
from argparse import ArgumentParser | |
import sys | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument('query', nargs='?', help='query string') | |
args = parser.parse_args() | |
qstr = unicode(args.query, sys.stdout.encoding) | |
r = requests.get('http://www.google.com/complete/search', params={'q':qstr, 'hl':'ja', 'ie':'utf_8', 'oe':'utf_8', 'output': 'toolbar'}) | |
root = etree.XML(r.text) | |
sugs = root.xpath('//suggestion') | |
sugstrs = [s.get('data') for s in sugs] | |
for ss in sugstrs: | |
print(ss) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment