A command line tool and module for Google API web and image search.
Project description
Install
Install Python
Install search_google via pip
pip install search_google
For the latest developer version, see Developer Install.
Usage
For help in the console:
search_google -h
Ensure that a CSE ID and a Google API developer key are set:
search_google -s cx="your_cse_id" search_google -s build_developerKey="your_dev_key"
Search the web for keyword “cat”:
search_google "cat" search_google "cat" --save_links=cat.txt search_google "cat" --save_downloads=downloads
Search for “cat” images:
search_google cat --searchType=image search_google "cat" --searchType=image --save_links=cat_images.txt search_google "cat" --searchType=image --save_downloads=downloads
Use as a Python module:
# Import the cse module for the results class
from search_google import cse
# Define buildargs for cse api
buildargs = {
'serviceName': 'customsearch',
'version': 'v1',
'developerKey': 'your_api_key'
}
# Define cseargs for search
cseargs = {
'q': 'keyword query',
'cx': 'your_cse_id',
'num': 3
}
# Create a results object
results = cse.results(buildargs, cseargs)
# Download the search results to a directory
results.download_links('downloads')
For more usage details, see the Documentation.
Developer Notes
Developer Install
Install the latest developer version with pip from github:
pip install git+https://github.com/rrwen/search_google
Install from git cloned source:
Ensure git is installed
Clone into current path
Install via pip
git clone https://github.com/rrwen/search_google cd search_google pip install . -I
Tests
Ensure unittest is available
Run tests
pip install . -I python -m unittest
Documentation Maintenance
Ensure sphinx is installed pip install -U sphinx
Update the documentation in docs/
pip install . -I sphinx-build -b html docs/source docs
Upload to github
Ensure git is installed
Add all files and commit changes
Push to github
git add . git commit -a -m "Generic update" git push
Upload to PyPi
Ensure twine is installed pip install twine
Ensure sphinx is installed pip install -U sphinx
Run tests and check for OK status
Delete dist directory
Update the version search_google/__init__.py
Update the documentation in docs/
Create source distribution
Upload to PyPi
pip install . -I python -m unittest sphinx-build -b html docs/source docs python setup.py sdist twine upload dist/*
Implementation
This command line tool uses the Google Custom Search Engine (CSE) to perform web and image searches. It relies on googleapiclient.build and cse.list, where build was used to create a Google API object and cse was used to perform the searches.
The class search_google.cse simply passed a dictionary of arguments into build and cse to process the returned results with properties and methods. search_google.cli was then used to create a command line interface for search_google.cse.
In order to use build and cse, a Google Developer API Key and a Google CSE ID needs to be created for API access (see search_google Setup). Creating these keys also required a Gmail account for login access.
googleapiclient.build <-- Google API | cse.list <-- Google CSE | search_google.cse <-- search results | search_google.cli <-- command line
A rough example is provided below thanks to the customsearch example from Google:
from apiclient.discovery import build
# Set developer key and CSE ID
dev_key = 'a_developer_key'
cse_id = 'a_cse_id'
# Obtain search results from Google CSE
service = build("customsearch", "v1", developerKey=dev_key)
results = service.cse().list(q='cat', cx=cse_id).execute()
# Manipulate search results after ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.