mirror of
https://github.com/karust/openserp.git
synced 2026-08-15 21:15:56 +08:00
Add OpenSERP SDK usage examples
This commit is contained in:
10
examples/search/python-keyword-csv/README.md
Normal file
10
examples/search/python-keyword-csv/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Keyword list to CSV (Python)
|
||||
|
||||
Searches a list of keywords and writes every result to `keywords.csv`.
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
python main.py
|
||||
```
|
||||
|
||||
Edit the `keywords` list in [main.py](main.py) to export your own.
|
||||
31
examples/search/python-keyword-csv/main.py
Normal file
31
examples/search/python-keyword-csv/main.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import csv
|
||||
|
||||
from openserp import OpenSERP
|
||||
|
||||
|
||||
keywords = ["search api", "open source serp"]
|
||||
output_file = "keywords.csv"
|
||||
|
||||
# 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=10)
|
||||
for result in response.results:
|
||||
rows.append(
|
||||
{
|
||||
"keyword": keyword,
|
||||
"rank": result.rank,
|
||||
"title": result.title,
|
||||
"url": result.url,
|
||||
"domain": result.domain,
|
||||
}
|
||||
)
|
||||
|
||||
with open(output_file, "w", newline="", encoding="utf-8") as handle:
|
||||
writer = csv.DictWriter(handle, fieldnames=["keyword", "rank", "title", "url", "domain"])
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
|
||||
print(f"Wrote {len(rows)} rows to {output_file}")
|
||||
1
examples/search/python-keyword-csv/requirements.txt
Normal file
1
examples/search/python-keyword-csv/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
openserp>=0.2.0,<1
|
||||
Reference in New Issue
Block a user