Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdbbf8d8

Browse files
committed
edited google custom search engine api tutorial
1 parentd6ff5e4 commitdbbf8d8

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

‎general/using-custom-search-engine-api/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
To run this:
33
-`pip3 install -r requirements.txt`
44
- 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:
5+
- Change`API_KEY` and`SEARCH_ENGINE_ID`variablesto yours, and then:
66
```
77
python search_engine.py "python"
88
```
@@ -25,4 +25,31 @@ To run this:
2525
Description: Looking for Python with a different OS? Python for Windows, Linux/UNIX, Mac OS
2626
X, Other. Want to help test development versions of Python? Prereleases ...
2727
URL: https://www.python.org/downloads/
28+
<..SNIPPED..>
29+
```
30+
- You can specify the page number, let's get 3rd result page for instance:
31+
```
32+
python search_engine.py "python" 3
33+
```
34+
Here is a **truncated output**:
35+
```
36+
========== Result #21 ==========
37+
Title: Python Tutorial - Tutorialspoint
38+
Description: Python is a general-purpose interpreted, interactive, object-oriented, and high-
39+
level programming language. It was created by Guido van Rossum during 1985-
40+
...
41+
URL: https://www.tutorialspoint.com/python/index.htm
42+
43+
========== Result #22 ==========
44+
Title: Google Python Style Guide
45+
Description: Python is the main dynamic language used at Google. This style guide is a list of
46+
dos and don'ts for Python programs. To help you format code correctly, we've ...
47+
URL: http://google.github.io/styleguide/pyguide.html
48+
49+
========== Result #23 ==========
50+
Title: Individual Edition | Anaconda
51+
Description: Open Source Anaconda Individual Edition is the world's most popular Python
52+
distribution platform with over 20 million users worldwide. You can trust in…
53+
URL: https://www.anaconda.com/products/individual
54+
<..SNIPPED..>
2855
```

‎general/using-custom-search-engine-api/search_engine.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@
22
importsys
33

44
# get the API KEY here: https://developers.google.com/custom-search/v1/overview
5-
API_KEY="<INSERT_YOUR_API_KEY_HERE>"
5+
API_KEY="AIzaSyCzSrJGrnLjkRovkjxvHTjaogMZX63MEo0"
66
# 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]
7+
SEARCH_ENGINE_ID="017479214723175900468:rx94qcojslh"
8+
# the search query you want, from the command line (e.g python search_engine.py 'python')
9+
try:
10+
query=sys.argv[1]
11+
except:
12+
print("Please specify a search query")
13+
exit()
14+
try:
15+
page=int(sys.argv[2])
16+
# make sure page is positive
17+
assertpage>0
18+
except:
19+
print("Page number isn't specified, defaulting to 1")
20+
page=1
1021
# constructing the URL
1122
# 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}"
23+
# calculating start, (page=2) => (start=11), (page=3) => (start=21)
24+
start= (page-1)*10+1
25+
url=f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start={start}"
1326

1427
# make the API request
1528
data=requests.get(url).json()
@@ -26,7 +39,7 @@
2639
# extract the page url
2740
link=search_item.get("link")
2841
# print the results
29-
print("="*10,f"Result #{i}","="*10)
42+
print("="*10,f"Result #{i+start-1}","="*10)
3043
print("Title:",title)
3144
print("Description:",snippet)
3245
print("URL:",link,"\n")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp