OT: SerpAPI

Started by spl, August 17, 2025, 10:11:57 AM

Previous topic - Next topic

spl

Google has an API to assist with searches. You can obtain a free auth key from SerpAPI.com. I queried for Jazz Restaurants in Raleigh NC. The json return was complex but I whittled it down to 3 elements with 3 returns
url="https://serpapi.com/search.json?q=Jazz Restaurant&location=Raleigh,+North Carolina,+United+States&hl=en&gl=us&google_domain=google.com&api_key=":mykey

title,address,description                                                                
The Big Easy NC,222 Fayetteville St,"Delicious food, incredible service, amazing vibe, great music!!!"         
Tin Roof,300 Glenwood Ave,"Great atmosphere, excellent service, comfortable place to hear live bands"
Moon Room,500 N West St Suite 150,"The vibes were awesome and the jazz band they had was phenomenal."       

I think the WB Json Extender can be very helpful describing a Json schema to assist with plucking out stuff you are interested in. Json is attached, just to show how complicated it can be.
Stan - formerly stanl [ex-Pundit]

spl

And, in case anyone is interested in the SerpAPI query in WB, pretty simple with WinHTTP. Code is below, just have to obtain a free auth key to test. You can change
q = "Jazz Restaurants near me"
location = "Raleigh,+North Carolina,+United+States"

to something different, like
q = "Pizza near me"
location = "Denver,+Colorado,+United+States"

I originally tried the code using SetRequestHeader() for the parameters, tried multiple variations for api_key and kept getting invalid api_key. So variable substitution to the rescue. Like I mentioned in the previous post, parsing the json might involve a little effort. That being said, this test is a tip of a bigger iceberg. You can use the 'engine' parameter to refine/specify queries and results. Once you are authorized the SerpAPI.com login has documentation and a playground. For a better peek at available engines see

https://deepwiki.com/serpapi/serpapi-python/4-supported-search-engines

Anyhoo... the code below works in WB

;SerpAPI test query - requires personal auth key
;Simple use of WinHttp
;
;Stan Littlefield 8/18/2025 (using WB2025C)
;===========================================================================

IntControl(73,1,0,0,0)
GoSub udfs
file = Dirscript():"JazzRaleigh.json"
key = [your SerpAPI auth key]
q = "Jazz Restaurants near me"
location = "Raleigh,+North Carolina,+United+States"
;Have to use substitution as SetRequestHeader() functions fail
Uri ="https://serpapi.com/search.json?q=%q%&location=%location%&hl=en&gl=us&google_domain=google.com&api_key=%key%"
Display(4,"SerpAPI Query",Uri)

oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHttp.SetTimeouts(50000, 50000, 50000, 50000);
oHttp.open("GET",Uri,"False")
oHttp.Send()

Response=oHttp.responseText
FilePut(file,Response)
If FileExist(file)
   Display(2,"Json Output Created",file)
   Run("notepad.exe",file)
Endif
oHTTP = 0
exit

:WBERRORHANDLER
geterror()
Terminate(@TRUE,"Error Encountered",errmsg)
;===========================================================================

:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine
Return
;===========================================================================
Stan - formerly stanl [ex-Pundit]

spl

And I realize - not very interesting looking for jazz or pizza. But consider something like
;SerpAPI test query - requires personal auth key
;Simple use of WinHttp - query for Winbatch Sample code
;
;Stan Littlefield 8/19/2025 (using WB2025C)
;===========================================================================

IntControl(73,1,0,0,0)
GoSub udfs
file = Dirscript():"WBSampleCode.json"
key = '[your key'
q = "Winbatch code examples"
eng = "google"
;Have to use substitution as SetRequestHeader() functions fail
Uri ="https://serpapi.com/search.json?q=%q%&engine=%eng%&api_key=%key%"
Display(4,"SerpAPI Query",Uri)

oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHttp.SetTimeouts(50000, 50000, 50000, 50000);
oHttp.open("GET",Uri,"False")
oHttp.Send()

Response=oHttp.responseText
FilePut(file,Response)
If FileExist(file)
   Display(2,"Json Output Created",file)
   Run("notepad.exe",file)
Endif
oHTTP = 0
exit

:WBERRORHANDLER
geterror()
Terminate(@TRUE,"Error Encountered",errmsg)
;===========================================================================

:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine
Return
;===========================================================================

that can be parsed like
title   : TechHome/WinBatch/Samples from Users
link    : https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Samples~from~Users
snippet : A comprehensive collection of Tech Support articles and sample code. Using the categories on the left, navigate through the site by selecting from the 
          list of ...

title   : The Complete WIL Tutorial
link    : https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/HTMLWIL_TCT001.htm
snippet : An additional variable, param0, gives you the total number of command-line parameters. Example: Passing parameters on the command line to a WinBatch 
          script.

title   : Quick Start Guide
link    : https://www.winbatch.com/quickstart.html
snippet : For a quick look at some WinBatch code, here are a few scripts presented for your perusal. You will see a lot more code samples in our Tech Support 
          Database. ; ...

title   : Running Programs
link    : https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/HTMLWIL_RP_001.htm
snippet : Example: · RunWait (program-name, parameters) · RunZoomWait (program-name, parameters) · RunIconWait (program-name, parameters) · RunShell 
          (program-name, parameters ...

title   : WinBatch Users Guide
link    : https://docs.winbatch.com/mergedProjects/WinBatch/WINBATCH_TOC000.htm
snippet : WinBatch controls Windows, Windows applications, and network connections. There are over 500 functions available to help you get the job done -- what 
          would take ...

title   : Using WinBatch to Automate a Stable32 Frequency ...
link    : http://www.wriley.com/WBT.htm
snippet : This paper describes how to use the WinBatch batch utility to automate a Stable32 frequency stability analysis.It contains several WinBatch example 
          programs, ...

title   : Working With Web Pages Tutorial
link    : https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+Tutorials+Working~With~Web~Pages.txt
snippet : This tutorial should give you a leg up when wandering into that world using Winbatch. These examples require you be familiar with Winbatch and have a 
          working ...

title   : WinBatch and Return Code Values
link    : https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DOS+WinBatch~and~Return~Code~Values~.txt
snippet : Here's another example: :toptop retcode=AskLine("hello", "Enter desired return code", 123) if !IsInt(retcode) then Message(("EEEK", "retcode must be 
          an ...

title   : Files and Directories
link    : https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/HTMLWIL_FAD001.htm
snippet : Use this function when you want to run a program which must be started from its own directory. ... Example: FileCopy("win.ini", "*.sav", @true) Run ...

title   : Getting Started with WinBatch
link    : https://www.webkitx.com/doc/Light/Getting%20Started%20with%20WinBatch.html
snippet : Featured in WinBatch are a simple BASIC-like syntax ... Because most of the work is already done for you in free sample code, you finish projects 
          faster.

... it gets more intriguing as there is an AI API element for queries. NOTE: the query only returns the first 10 'positions' but there are start and num parameters, and switch the engine to 'bing' for comparison.

Won't bore anymore with additional posts and this thread can die.
Stan - formerly stanl [ex-Pundit]

td

I can't speak for others, but I found it interesting. Maybe put it in a Tech Support article?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

This is super interesting Stan.  Not sure how yet, but super interesting.
Thanks for posting.
The mind is everything; What you think, you become.

spl

Thank you both. I look at stuff like this more as a skill rather than a programming exercise. Gone are the days (in my opinion) for analyst/programmer positions that could really benefit from a scripting language. You might find a mom and pop wanting to upgrade from Excel for short-term, but firms and recruiters (based on the 10-30 emails I get every other day asking my interest... 1 reason to hate LinkedIn and Indeed)... want skills for marketing and sales that require knowledge to use AI-like mechanisms parsing huge queries into fashionable metrics.

To that point. I will note that SerpAPI can also export as html (but not really practical); but can be integrated into other API's like WebUI or Deep Seek.

So playing with the free auth, might enhance skills to work for/with players who see benefit in paying for the app.

just .02
Stan - formerly stanl [ex-Pundit]

kdmoyers

On reflection, I could possibly see it being used by me to get daily numbers for the marketing wonks about our serp position for various keywords.  You can pay a ton for that data from the big marketing services, but you will never know if they are telling the truth.  I'll wait until the wonks make a case for it however, as there are already dozens of irons in the fire now.
The mind is everything; What you think, you become.

td

Speaking of AI. I have been experimenting with AI as a code generator for complex problems of late (not WinBatch/WILL).  Some LLMs are better than others, but the overall results have been less than spectacular. In the best cases, it is about as useful as a search engine using a well crafted query. IMHO, the quality of software, Web based or otherwise, is in fairly obvious decline. Perhaps AI has something to do with it?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Me too.   At times they spit out a brilliant piece of code and then they seem to go stupid.  Some times they completely change the topic and spit out code completely unrelated to the question or chain of questions or they go in circles.   ChatGPT 5.0 seems much worse than its predecessor.

I have to admit though that they are helpful, as long as you don't trust them too much.

Jim

spl

Normally I get accused of hijacking a thread, but this was not intent to be a discussion about code generation, but more about data gathering in general. The AI 'elements' (again in my opinion) are looking at harvesting/semi-forecasting. If you are stuck on AI as a tool to make your code better, or write it for you you are probably stuck.
Stan - formerly stanl [ex-Pundit]

td

While my computer background is in software/electrical engineering, I still remember the heydays of the "programmer/analyst" title. I didn't know that it was still in use. But the "programmer" part of the "analyst/programmer" comment did generate my hijack. My apologies.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

No apologies necessary. Probably showing my age. I must note the Python folks seem to have the edge with SerpAPI. Didn't realize you could code for engine = 'walmart' and others. I tried a couple with engine = 'duckduckgo' and got decent results. Also getting more familiar with parsing json elements like [organic_results] [pagination]. All in all a fun learning experience.
Stan - formerly stanl [ex-Pundit]

spl

Without further adieu.. there is a SerpAPI Playground you should be able to try for free without api key. Throw this in your browser

https://serpapi.com/playground?engine=home_depot&q=mulch&location=Raleigh,+North Carolina,+United+States

Should come back with 3 tiered display (1) Search parameters (2) google display of mulch (3) json code. NOTE: the url will remove location= and change to country=us

Unfortunately, while trying the http script code I previously posted will work with just the url, the json return is not what you would expect, although there is a delivery_zip parameter you might want to integrate into the url

Stan - formerly stanl [ex-Pundit]

SMF spam blocked by CleanTalk