Skip to content

Instantly share code, notes, and snippets.

@fpt
Last active January 26, 2022 16:02
Show Gist options
  • Save fpt/b21b8abea239392129b6 to your computer and use it in GitHub Desktop.
Save fpt/b21b8abea239392129b6 to your computer and use it in GitHub Desktop.
Google Suggest API example
#!/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