Created
October 31, 2014 11:25
-
-
Save hiway/2c7b457b93f0efd5d66b to your computer and use it in GitHub Desktop.
Fetch top X URLs for keywords from 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
#!/usr/bin/env python | |
"""Fetch top X URLs for keywords from Google.""" | |
import click # [sudo] pip install click | |
import requests # [sudo] pip install requests | |
search_url = u'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s' | |
def get_urls_from_search(keywords, number): | |
"""You can import this function into your own code and use it | |
to fetch URLs. It will return a list that you can use as you | |
wish. | |
example: | |
>>> from google import get_urls_from_search | |
>>> get_urls_from_search('hello world', 1) | |
[u'http://en.wikipedia.org/wiki/%2522Hello,_world!%2522_program'] | |
""" | |
# Allow function to be used by passing list, tuples or string | |
if type(keywords) != str: | |
assert type(keywords) == tuple or type(keywords) == list | |
keywords = ' '.join(keywords) | |
# Call Google search API | |
response = requests.get(search_url % keywords) | |
# list comprehension to pick out only the URLs from JSON data structure | |
urls = [x['url'] for x in response.json()[u'responseData'][u'results']] | |
# return as many urls as requested (with maximum being 8, that Google gives) | |
return urls[:number] | |
@click.command() | |
@click.option('--n', default=1, help='number of links to fetch') | |
@click.argument('keywords', nargs=-1) | |
def command_line(keywords, n): | |
# Print the URLs, one on each line | |
print('\n'.join(get_urls_from_search(keywords, n))) | |
if __name__ == '__main__': | |
"""Usage: | |
python google_links.py some keywords | |
python google_links.py some keywords --n=3 | |
""" | |
command_line() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment