I have a snippet of code using the pygoogle python module that allows me to programmatically search for some term in google succintly:
g = pygoogle(search_term) g.pages = 1 results = g.get_urls()[0:10]
I just found out that this has been discontinued unfortunately and replaced by something called the google custom search. I looked at the other related questions on SO but didn't find anything I could use. I have two questions:
1) Does google custom search allow me to do exactly what I am doing in the three lines above?
2) If yes - where can I find example code to do exactly what I am doing above? If no then what is the alternative to do what I did using pygoogle?
Custom Search JSON API requires the use of an API key. An API key is a way to identify your client to Google. After you have an API key, your application can append the query parameter key=yourAPIKey to all request URLs. The API key is safe for embedding in URLs, it doesn't need any encoding.
Yes, Google provides a search API that you can use: The Google AJAX Search API lets you put Google Search in your web pages with JavaScript. You can embed a simple, dynamic search box and display search results in your own web pages or use the results in innovative, programmatic ways.
It is possible to do this. The setup is... not very straightforward, but the end result is that you can search the entire web from python with few lines of code.
There are 3 main steps in total.
The pygoogle's page states:
Unfortunately, Google no longer supports the SOAP API for search, nor do they provide new license keys. In a nutshell, PyGoogle is pretty much dead at this point.
You can use their AJAX API instead. Take a look here for sample code: http://dcortesi.com/2008/05/28/google-ajax-search-api-example-python-code/
... but you actually can't use AJAX API either. You have to get a Google API key. https://developers.google.com/api-client-library/python/guide/aaa_apikeys For simple experimental use I suggest "server key".
Indeed, the old API is not available. The best new API that is available is Custom Search. It seems to support only searching within specific domains, however, after following this SO answer you can search the whole web:
- From the Google Custom Search homepage ( http://www.google.com/cse/ ), click Create a Custom Search Engine.
- Type a name and description for your search engine.
- Under Define your search engine, in the Sites to Search box, enter at least one valid URL (For now, just put www.anyurl.com to get past this screen. More on this later ).
- Select the CSE edition you want and accept the Terms of Service, then click Next. Select the layout option you want, and then click Next.
- Click any of the links under the Next steps section to navigate to your Control panel.
- In the left-hand menu, under Control Panel, click Basics.
- In the Search Preferences section, select Search the entire web but emphasize included sites.
- Click Save Changes.
- In the left-hand menu, under Control Panel, click Sites.
- Delete the site you entered during the initial setup process.
This approach is also recommended by Google: https://support.google.com/customsearch/answer/2631040
pip install google-api-python-client
, more info here:
So, after setting this up, you can follow the code samples from few places:
simple example: https://github.com/google/google-api-python-client/blob/master/samples/customsearch/main.py
cse()
function docs: https://google-api-client-libraries.appspot.com/documentation/customsearch/v1/python/latest/customsearch_v1.cse.html
and end up with this:
from googleapiclient.discovery import build import pprint my_api_key = "Google API key" my_cse_id = "Custom Search Engine ID" def google_search(search_term, api_key, cse_id, **kwargs): service = build("customsearch", "v1", developerKey=api_key) res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute() return res['items'] results = google_search( 'stackoverflow site:en.wikipedia.org', my_api_key, my_cse_id, num=10) for result in results: pprint.pprint(result)
After some tweaking you could write some functions that behave exactly like your snippet, but I'll skip this step here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With