Repository navigation
Expand file tree
/
Copy pathserp.py
More file actions
61 lines (49 loc) · 1.93 KB
/
Copy pathserp.py
File metadata and controls
61 lines (49 loc) · 1.93 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Fetch Google search results with the Semust SERP API.
pip install requests
export SEMUST_API_USER=... SEMUST_API_PASSWORD=...
python serp.py "best seo tools"
"""
import json
import os
import sys
import time
import requests
API = "https://data.semust.com"
HEADERS = {
"SEMUST-API-USER": os.environ["SEMUST_API_USER"],
"SEMUST-API-PASSWORD": os.environ["SEMUST_API_PASSWORD"],
}
WAIT = 60 # seconds the API holds the request open before answering 202
def call(path, payload):
"""POST a request and return the final response, polling if the API answers 202."""
resp = requests.post(f"{API}{path}", params={"wait": WAIT}, json=payload,
headers=HEADERS, timeout=WAIT + 30)
while resp.status_code == 202:
task = resp.json()
time.sleep(int(resp.headers.get("Retry-After", 5)))
resp = requests.get(task["poll_url"], headers=HEADERS, timeout=30)
return resp
def main():
keyword = sys.argv[1] if len(sys.argv) > 1 else "best seo tools"
resp = call("/v1/serp", {
"keyword": keyword,
"language": "en", # interface language (ISO 639-1)
"country": "US", # results country (ISO 3166-1 alpha-2)
"format": "json", # "raw" returns the page HTML instead
"mobile": False,
"start_page": 1,
"end_page": 1,
})
if resp.status_code != 200:
sys.exit(f"Error {resp.status_code}: {resp.text}")
data = resp.json()
for result in data.get("organic", []):
print(f"{result['rank']:>2}. {result['title']}\n {result['link']}")
questions = [q["question"] for q in data.get("people_also_ask", [])]
if questions:
print("\nPeople also ask:\n - " + "\n - ".join(questions))
with open("serp_result.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print("\nFull response saved to serp_result.json")
if __name__ == "__main__":
main()