-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_suggestions.py
More file actions
executable file
·74 lines (57 loc) · 2.04 KB
/
Copy pathgoogle_suggestions.py
File metadata and controls
executable file
·74 lines (57 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
'''
usage: google_suggestions.py [-h] [query [query ...]]
positional arguments:
query search string
optional arguments:
-h, --help show this help message and exit
library usage:
>>> from google_suggestions import suggest
>>> suggest('some')
['something in the water', 'some da eat', 'someone you loved', 'something in the water 2020',
'something in the water 2020 lineup', 'something to eat', 'someone you loved lyrics',
'something in the water 2020 tickets', 'somewhere over the rainbow', 'something in the water tickets']
'''
import requests
import json
from argparse import ArgumentParser
DEFAULT = {
'suggestURL': 'http://suggestqueries.google.com/complete/search?client=firefox&q=',
'headers': {'User-agent':'Mozilla/5.0'}
}
def suggest(query):
'''Get list of top 10 Google suggestions
Args:
query(str): Query for Google suggestions
Returns:
list: a list of Google search suggestions as strings
'''
URL = DEFAULT['suggestURL'] + query
response = requests.get(URL, headers=DEFAULT['headers'])
return json.loads(response.content.decode('utf-8'))[1]
def cli_args_string(parser = ArgumentParser()):
'''Returns space seperated string of all command line arguments
Args:
parser(ArgumentParser): parser for cli arguments
Returns:
string: all command line arguments
'''
parser.add_argument('query', nargs='*', help='search string')
return ' '.join(parser.parse_args().query)
def googleSearch(query, url = 'https://www.google.com'):
'''Returns URL as a string for the Google Search URL
Args:
string: search terms
string: URL for search engine
(default: https://www.google.com/)
Returns:
string: URL for search results
'''
with requests.session() as session:
return requests.get(url, params={'q': query}).url
def main(args = cli_args_string()):
if args:
for result in suggest(args):
print(result)
if __name__ == '__main__':
main()