Skip to content

Commit bbbe975

Browse files
VinciGit00claude
andcommitted
feat: add locationGeoCode option to searchScraper for geo-targeted search
Add optional locationGeoCode parameter to the searchScraper function that allows targeting search results from a specific geographic location. This implements the API changes from ScrapeGraphAI/sgai-api#384. - Add locationGeoCode to options destructuring - Add location_geo_code to API payload when provided - Update JSDoc documentation - Update README with parameter documentation and usage example Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 58c3a2a commit bbbe975

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,28 @@ const prompt = 'What is the latest version of Python and what are its main featu
416416
})();
417417
```
418418

419+
#### Geo-Targeted Search
420+
421+
Use `locationGeoCode` to target search results from a specific geographic location:
422+
423+
```javascript
424+
import { searchScraper } from 'scrapegraph-js';
425+
426+
const apiKey = 'your-api-key';
427+
const prompt = 'Best restaurants near me';
428+
429+
(async () => {
430+
try {
431+
const response = await searchScraper(apiKey, prompt, 5, null, null, {
432+
locationGeoCode: 'us' // Search results targeted to United States
433+
});
434+
console.log(response.result);
435+
} catch (error) {
436+
console.error('Error:', error);
437+
}
438+
})();
439+
```
440+
419441
### Crawl API
420442

421443
Start a crawl job to extract structured data from a website and its linked pages, using a custom schema.
@@ -1122,6 +1144,7 @@ Searches and extracts information from multiple web sources using AI.
11221144
- `stealth` (boolean): Enable stealth mode
11231145
- `extractionMode` (boolean): Whether to use AI extraction
11241146
- `renderHeavyJs` (boolean): Whether to render heavy JavaScript
1147+
- `locationGeoCode` (string): The geo code of the location to search in (e.g., "us", "gb", "de")
11251148
- `mock` (boolean): Override mock mode for this request
11261149

11271150
**Returns:** Promise that resolves to an object containing:

src/searchScraper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import { getMockResponse } from './utils/mockResponse.js';
2121
* @param {boolean} [options.extractionMode=true] - Whether to use AI extraction (true) or markdown conversion (false).
2222
* AI extraction costs 10 credits per page, markdown conversion costs 2 credits per page.
2323
* @param {boolean} [options.stealth=false] - Enable stealth mode to avoid bot detection
24+
* @param {string} [options.locationGeoCode=null] - The geo code of the location to search in (e.g., "us", "gb", "de")
2425
* @returns {Promise<string>} Extracted data in JSON format matching the provided schema
2526
* @throws - Will throw an error in case of an HTTP failure.
2627
*/
2728
export async function searchScraper(apiKey, prompt, numResults = 3, schema = null, userAgent = null, options = {}) {
28-
const { mock = null, renderHeavyJs = false, extractionMode = true, stealth = false } = options;
29+
const { mock = null, renderHeavyJs = false, extractionMode = true, stealth = false, locationGeoCode = null } = options;
2930

3031
// Check if mock mode is enabled
3132
const useMock = mock !== null ? mock : isMockEnabled();
@@ -61,6 +62,10 @@ export async function searchScraper(apiKey, prompt, numResults = 3, schema = nul
6162
payload.stealth = stealth;
6263
}
6364

65+
if (locationGeoCode) {
66+
payload.location_geo_code = locationGeoCode;
67+
}
68+
6469
if (schema) {
6570
if (schema instanceof ZodType) {
6671
payload.output_schema = zodToJsonSchema(schema);

0 commit comments

Comments
 (0)