88 playwright_install ,
99 add_download_options
1010)
11- from scrapegraph_py import Client
12- from scrapegraph_py .logger import sgai_logger
1311
1412st .set_page_config (page_title = "Scrapegraph-ai demo" , page_icon = "🕷️" )
1513
1614# Install playwright browsers
1715playwright_install ()
1816
19- # Initialize logger
20- sgai_logger .set_logging (level = "INFO" )
21-
2217def save_email (email ):
2318 with open ("mails.txt" , "a" ) as file :
2419 file .write (email + "\n " )
@@ -45,35 +40,43 @@ def save_email(email):
4540left_co , cent_co , last_co = st .columns (3 )
4641with cent_co :
4742 st .image ("assets/scrapegraphai_logo.png" )
43+ st .title ('Scrapegraph-api' )
44+ st .write ("refill at this page" )
4845
49- # Use password input for API key to mask it
50- st .write ("### You can buy the API credits [here](https://scrapegraphai.com)" )
51-
52- api_key = st .text_input ('Enter your API key:' , type = "password" , help = "API key must start with 'sgai-'" )
46+ # Get the API key, URL, prompt, and optional schema from the user
47+ api_key = st .text_input ('Enter your API key:' )
5348url = st .text_input ('Enter the URL to scrape:' )
5449prompt = st .text_input ('Enter your prompt:' )
50+ schema = st .text_input ('Enter your optional schema (leave blank if not needed):' )
5551
5652# When the user clicks the 'Scrape' button
5753if st .button ('Scrape' ):
58- if not api_key .startswith ('sgai-' ):
59- st .error ("Invalid API key format. API key must start with 'sgai-'" )
60- elif not url :
61- st .error ("Please enter a URL to scrape" )
62- elif not prompt :
63- st .error ("Please enter a prompt" )
54+ # Set up the headers and payload for the API request
55+ headers = {'Content-Type' : 'application/json' }
56+ payload = {
57+ 'api_key' : api_key ,
58+ 'url' : url ,
59+ 'prompt' : prompt ,
60+ 'schema' : schema
61+ }
62+
63+ # Make the API request
64+ response = requests .post ('https://api.scrapegraphai.com/smart_scraper' , headers = headers , data = json .dumps (payload ))
65+
66+ # If the request was successful
67+ if response .status_code == 200 :
68+ # Parse the JSON response
69+ data = response .json ()
70+
71+ # Display the extracted data
72+ st .write (data ['result' ])
73+
74+ # Display the remaining credits
75+ st .write (f"Remaining credits: { data ['credits_left' ]} " )
76+
77+ # If the request was unsuccessful
6478 else :
65- try :
66- sgai_client = Client (api_key = api_key )
67- response = sgai_client .smartscraper (
68- website_url = url ,
69- user_prompt = prompt
70- )
71- st .write (f"Request ID: { response ['request_id' ]} " )
72- st .write (f"Result: { response ['result' ]} " )
73- except Exception as e :
74- st .error (f"Error: { str (e )} " )
75- finally :
76- sgai_client .close ()
79+ st .write (f"Error: { response .status_code } " )
7780
7881
7982left_co2 , * _ , cent_co2 , last_co2 , last_c3 = st .columns ([1 ] * 18 )
0 commit comments