Get complete SERP data for a keyword Args: keyword: Search keyword location_code: DataForSEO location code limit: Number of results to return Returns: Dict with SERP data including all ranking pages
(
self, keyword: str, location_code: int = 2840, limit: int = 100
)
| 134 | return results |
| 135 | |
| 136 | def get_serp_data( |
| 137 | self, keyword: str, location_code: int = 2840, limit: int = 100 |
| 138 | ) -> Dict[str, Any]: |
| 139 | """ |
| 140 | Get complete SERP data for a keyword |
| 141 | |
| 142 | Args: |
| 143 | keyword: Search keyword |
| 144 | location_code: DataForSEO location code |
| 145 | limit: Number of results to return |
| 146 | |
| 147 | Returns: |
| 148 | Dict with SERP data including all ranking pages |
| 149 | """ |
| 150 | data = [ |
| 151 | { |
| 152 | "keyword": keyword, |
| 153 | "location_code": location_code, |
| 154 | "language_code": "en", |
| 155 | "device": "desktop", |
| 156 | "os": "windows", |
| 157 | "depth": limit, |
| 158 | } |
| 159 | ] |
| 160 | |
| 161 | response = self._post("/v3/serp/google/organic/live/advanced", data) |
| 162 | |
| 163 | if response["status_code"] != 20000: |
| 164 | return {"error": "API request failed"} |
| 165 | |
| 166 | task = self._first_task(response) |
| 167 | if not task or task.get("status_code") != 20000: |
| 168 | return {"error": "Task failed"} |
| 169 | |
| 170 | result = self._first_result(task) |
| 171 | if result is None: |
| 172 | return {"error": "Task returned no results"} |
| 173 | |
| 174 | # Extract organic results |
| 175 | organic_results = [] |
| 176 | for item in result.get("items", []): |
| 177 | if item["type"] == "organic": |
| 178 | organic_results.append( |
| 179 | { |
| 180 | "position": item.get("rank_absolute"), |
| 181 | "url": item.get("url"), |
| 182 | "domain": item.get("domain"), |
| 183 | "title": item.get("title"), |
| 184 | "description": item.get("description"), |
| 185 | "breadcrumb": item.get("breadcrumb"), |
| 186 | } |
| 187 | ) |
| 188 | |
| 189 | # Extract SERP features |
| 190 | features = [] |
| 191 | for item in result.get("items", []): |
| 192 | if item["type"] != "organic": |
| 193 | features.append(item["type"]) |
no test coverage detected