-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_search_scraper.py
More file actions
37 lines (25 loc) · 1.05 KB
/
Copy pathgoogle_search_scraper.py
File metadata and controls
37 lines (25 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Run the hosted Google Search SERP Scraper and print Dataset items."""
from __future__ import annotations
import json
import os
from collections.abc import Iterator
from pathlib import Path
from typing import Any
from apify_client import ApifyClient
ACTOR_ID = "datascraperes/google-serp-scraper"
ROOT = Path(__file__).resolve().parents[2]
INPUT_PATH = ROOT / "data" / "sample-input.json"
def run_google_search(run_input: dict[str, Any]) -> Iterator[dict[str, Any]]:
"""Run the hosted Actor and yield default Dataset items."""
token = os.environ.get("APIFY_API_TOKEN")
if not token:
raise RuntimeError("Set APIFY_API_TOKEN before running this example.")
client = ApifyClient(token)
run = client.actor(ACTOR_ID).call(run_input=run_input)
yield from client.dataset(run["defaultDatasetId"]).iterate_items()
def main() -> None:
run_input = json.loads(INPUT_PATH.read_text(encoding="utf-8"))
for item in run_google_search(run_input):
print(json.dumps(item, ensure_ascii=False))
if __name__ == "__main__":
main()