Skip to content

Commit 2aabf0f

Browse files
committed
added using cse tutorial
1 parent 21900ef commit 2aabf0f

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
6363
- [How to Get the Size of Directories in Python](https://www.thepythoncode.com/article/get-directory-size-in-bytes-using-python). ([code](general/calculate-directory-size))
6464
- [How to Download Torrent Files in Python](https://www.thepythoncode.com/article/download-torrent-files-in-python). ([code](general/torrent-downloader))
6565
- [How to Play and Record Audio in Python](https://www.thepythoncode.com/article/play-and-record-audio-sound-in-python). ([code](general/recording-and-playing-audio))
66+
- [How to Use Google Custom Search Engine API in Python](https://www.thepythoncode.com/article/use-google-custom-search-engine-api-in-python). ([code](general/using-custom-search-engine-api))
6667

6768
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
6869
- [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor))

Diff for: general/using-custom-search-engine-api/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [How to Use Google Custom Search Engine API in Python](https://www.thepythoncode.com/article/use-google-custom-search-engine-api-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- You need to setup a CSE account, check [the tutorial](https://www.thepythoncode.com/article/use-google-custom-search-engine-api-in-python) on how you can set up one.
5+
- Change `API_KEY` and `SEARCH_ENGINE_ID` to yours, and then:
6+
```
7+
python search_engine.py "python"
8+
```
9+
This will use the query "python" to search for results, here is a cropped output:
10+
```
11+
========== Result #1 ==========
12+
Title: Welcome to Python.org
13+
Description: The official home of the Python Programming Language.
14+
URL: https://www.python.org/
15+
16+
========== Result #2 ==========
17+
Title: The Python Tutorial — Python 3.8.2 documentation
18+
Description: It has efficient high-level data structures and a simple but effective approach to
19+
object-oriented programming. Python's elegant syntax and dynamic typing,
20+
together ...
21+
URL: https://docs.python.org/3/tutorial/
22+
23+
========== Result #3 ==========
24+
Title: Download Python | Python.org
25+
Description: Looking for Python with a different OS? Python for Windows, Linux/UNIX, Mac OS
26+
X, Other. Want to help test development versions of Python? Prereleases ...
27+
URL: https://www.python.org/downloads/
28+
```
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import requests
2+
import sys
3+
4+
# get the API KEY here: https://developers.google.com/custom-search/v1/overview
5+
API_KEY = "<INSERT_YOUR_API_KEY_HERE>"
6+
# get your Search Engine ID on your CSE control panel
7+
SEARCH_ENGINE_ID = "<INSERT_YOUR_SEARCH_ENGINE_ID_HERE>"
8+
# the search query you want, from the command line
9+
query = sys.argv[1]
10+
# constructing the URL
11+
# doc: https://developers.google.com/custom-search/v1/using_rest
12+
url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}"
13+
14+
# make the API request
15+
data = requests.get(url).json()
16+
# get the result items
17+
search_items = data.get("items")
18+
# iterate over 10 results found
19+
for i, search_item in enumerate(search_items, start=1):
20+
# get the page title
21+
title = search_item.get("title")
22+
# page snippet
23+
snippet = search_item.get("snippet")
24+
# alternatively, you can get the HTML snippet (bolded keywords)
25+
html_snippet = search_item.get("htmlSnippet")
26+
# extract the page url
27+
link = search_item.get("link")
28+
# print the results
29+
print("="*10, f"Result #{i}", "="*10)
30+
print("Title:", title)
31+
print("Description:", snippet)
32+
print("URL:", link, "\n")

0 commit comments

Comments
 (0)