1+ import axios from 'axios' ;
2+
3+ /**
4+ * Scrape and extract structured data from a webpage using ScrapeGraph AI.
5+ *
6+ * @param {string } apiKey - Your ScrapeGraph AI API key
7+ * @param {string } url - The URL of the webpage to scrape
8+ * @param {string } prompt - Natural language prompt describing what data to extract
9+ * @param {Object } [schema] - Optional schema object defining the output structure
10+ * @returns {Promise<string> } Extracted data in JSON format matching the provided schema
11+ */
12+ export async function smartScraper ( apiKey , url , prompt , schema = null ) {
13+ const endpoint = "https://api.scrapegraphai.com/v1/smartscraper" ;
14+ const headers = {
15+ "accept" : "application/json" ,
16+ "SGAI-APIKEY" : apiKey ,
17+ "Content-Type" : "application/json"
18+ } ;
19+
20+ const payload = {
21+ website_url : url ,
22+ user_prompt : prompt
23+ } ;
24+
25+ if ( schema ) {
26+ payload . output_schema = {
27+ description : schema . title || "Schema" ,
28+ name : schema . title || "Schema" ,
29+ properties : schema . properties || { } ,
30+ required : schema . required || [ ]
31+ } ;
32+ }
33+
34+ try {
35+ const response = await axios . post ( endpoint , payload , { headers } ) ;
36+ return JSON . stringify ( response . data ) ;
37+ } catch ( error ) {
38+ if ( error . response ) {
39+ if ( error . response . status === 403 ) {
40+ return JSON . stringify ( {
41+ error : "Access forbidden (403)" ,
42+ message : "You do not have permission to access this resource."
43+ } ) ;
44+ }
45+ return JSON . stringify ( {
46+ error : "HTTP error occurred" ,
47+ message : error . message ,
48+ status_code : error . response . status
49+ } ) ;
50+ }
51+ return JSON . stringify ( {
52+ error : "An error occurred" ,
53+ message : error . message
54+ } ) ;
55+ }
56+ }
57+
58+ /**
59+ * Retrieve the status or the result of a scraping request. It also allows you to see the result of old requests.
60+ *
61+ * @param {string } apiKey - Your ScrapeGraph AI API key
62+ * @param {string } requestId - The request ID associated with the feedback
63+ * @returns {Promise<string> } Information related to the status or result of a scraping request.
64+ */
65+ export async function smartScraperInfo ( apiKey , requestId ) {
66+ const endpoint = "https://api.scrapegraphai.com/v1/smartscraper/" + requestId ;
67+ const headers = {
68+ "accept" : "application/json" ,
69+ "SGAI-APIKEY" : apiKey ,
70+ } ;
71+
72+ try {
73+ const response = await axios . get ( endpoint , { headers } ) ;
74+ return JSON . stringify ( response . data )
75+ } catch ( error ) {
76+ if ( error . response ) {
77+ if ( error . response . status === 403 ) {
78+ return JSON . stringify ( {
79+ error : "Access forbidden (403)" ,
80+ message : "You do not have permission to access this resource."
81+ } ) ;
82+ }
83+ return JSON . stringify ( {
84+ error : "HTTP error occurred" ,
85+ message : error . message ,
86+ status_code : error . response . status
87+ } ) ;
88+ }
89+ return JSON . stringify ( {
90+ error : "An error occurred" ,
91+ message : error . message
92+ } ) ;
93+ }
94+
95+ }
0 commit comments