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

Commit64d5b81

Browse files
committed
added get google page ranking tutorial
1 parente324bc7 commit64d5b81

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
112112
-[How to Use Google Drive API in Python](https://www.thepythoncode.com/article/using-google-drive--api-in-python). ([code](general/using-google-drive-api))
113113
-[How to Translate Text in Python](https://www.thepythoncode.com/article/translate-text-in-python). ([code](general/using-google-translate-api))
114114
-[How to Make a URL Shortener in Python](https://www.thepythoncode.com/article/make-url-shortener-in-python). ([code](general/url-shortener))
115+
-[How to Get Google Page Ranking in Python](https://www.thepythoncode.com/article/get-google-page-ranking-by-keyword-in-python). ([code](general/getting-google-page-ranking))
115116

116117
-###[Database](https://www.thepythoncode.com/topic/using-databases-in-python)
117118
-[How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[How to Get Google Page Ranking in Python](https://www.thepythoncode.com/article/get-google-page-ranking-by-keyword-in-python)
2+
To run this:
3+
-`pip3 install -r requirements.txt`
4+
- Setup CSE API and retrieve`API_KEY` and`SEARCH_ENGINE_ID` as shown in[this tutorial](https://www.thepythoncode.com/article/use-google-custom-search-engine-api-in-python) and replace them in`page_ranking.py` script.
5+
- For instance, to get the page rank of`thepythoncode.com` of the keyword "google custom search engine api python":
6+
```
7+
python page_ranking.py thepythoncode.com "google custom search engine api python"
8+
```
9+
**Output:**
10+
```
11+
[*] Going for page: 1
12+
[+] thepythoncode.com is found on rank #3 for keyword: 'google custom search engine api python'
13+
[+] Title: How to Use Google Custom Search Engine API in Python - Python ...
14+
[+] Snippet: 10 results ... Learning how to create your own Google Custom Search Engine and use its
15+
Application Programming Interface (API) in Python.
16+
[+] URL: https://www.thepythoncode.com/article/use-google-custom-search-engine-api-in-python
17+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
importrequests
2+
importurllib.parseasp
3+
importsys
4+
5+
# get the API KEY here: https://developers.google.com/custom-search/v1/overview
6+
API_KEY="<INSERT_YOUR_API_KEY_HERE>"
7+
# get your Search Engine ID on your CSE control panel
8+
SEARCH_ENGINE_ID="<INSERT_YOUR_SEARCH_ENGINE_ID_HERE>"
9+
# target domain you want to track
10+
target_domain=sys.argv[1]
11+
# target keywords
12+
query=sys.argv[2]
13+
14+
forpageinrange(1,11):
15+
print("[*] Going for page:",page)
16+
# calculating start
17+
start= (page-1)*10+1
18+
# make API request
19+
url=f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start={start}"
20+
data=requests.get(url).json()
21+
search_items=data.get("items")
22+
# a boolean that indicates whether `target_domain` is found
23+
found=False
24+
fori,search_iteminenumerate(search_items,start=1):
25+
# get the page title
26+
title=search_item.get("title")
27+
# page snippet
28+
snippet=search_item.get("snippet")
29+
# alternatively, you can get the HTML snippet (bolded keywords)
30+
html_snippet=search_item.get("htmlSnippet")
31+
# extract the page url
32+
link=search_item.get("link")
33+
# extract the domain name from the URL
34+
domain_name=p.urlparse(link).netloc
35+
ifdomain_name.endswith(target_domain):
36+
# get the page rank
37+
rank=i+start-1
38+
print(f"[+]{target_domain} is found on rank #{rank} for keyword: '{query}'")
39+
print("[+] Title:",title)
40+
print("[+] Snippet:",snippet)
41+
print("[+] URL:",link)
42+
# target domain is found, exit out of the program
43+
found=True
44+
break
45+
iffound:
46+
break
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp