mirror of
https://github.com/karust/openserp.git
synced 2026-08-16 21:36:06 +08:00
Add OpenSERP SDK usage examples
This commit is contained in:
11
examples/seo/python-rank-tracker/README.md
Normal file
11
examples/seo/python-rank-tracker/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Rank tracker (Python)
|
||||
|
||||
Checks where a target domain ranks for a list of keywords and writes the results
|
||||
to `rankings.csv`. Handles `www.` and subdomains when matching the domain.
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
python main.py
|
||||
```
|
||||
|
||||
Edit `target_domain` and `keywords` in [main.py](main.py) to track your own site.
|
||||
38
examples/seo/python-rank-tracker/main.py
Normal file
38
examples/seo/python-rank-tracker/main.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import csv
|
||||
|
||||
from openserp import OpenSERP
|
||||
|
||||
target_domain = "go.dev"
|
||||
keywords = ["golang tutorial", "go programming language", "golang documentation"]
|
||||
output_file = "rankings.csv"
|
||||
|
||||
|
||||
def matches(domain: str | None, target: str) -> bool:
|
||||
if not domain:
|
||||
return False
|
||||
domain = domain.lower().removeprefix("www.")
|
||||
return domain == target or domain.endswith(f".{target}")
|
||||
|
||||
|
||||
# Hosted API instead? Get a key at https://openserp.org/dashboard/keys:
|
||||
# with OpenSERP(api_key="<YOUR_API_TOKEN>") as client:
|
||||
with OpenSERP(base_url="http://localhost:7000") as client:
|
||||
rows = []
|
||||
for keyword in keywords:
|
||||
response = client.search(engine="google", text=keyword, region="US", limit=20)
|
||||
hit = next((r for r in response.results if matches(r.domain, target_domain)), None)
|
||||
rows.append(
|
||||
{
|
||||
"keyword": keyword,
|
||||
"rank": hit.rank if hit else "not in top 20",
|
||||
"url": hit.url if hit else "",
|
||||
}
|
||||
)
|
||||
print(f'"{keyword}": {rows[-1]["rank"]}')
|
||||
|
||||
with open(output_file, "w", newline="", encoding="utf-8") as handle:
|
||||
writer = csv.DictWriter(handle, fieldnames=["keyword", "rank", "url"])
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
|
||||
print(f"\nSaved rankings for {target_domain} to {output_file}")
|
||||
1
examples/seo/python-rank-tracker/requirements.txt
Normal file
1
examples/seo/python-rank-tracker/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
openserp>=0.2.0,<1
|
||||
Reference in New Issue
Block a user