Is There An Api For The Google Answer Boxes?
Solution 1:
There is an instant answer api available from DuckDuckGo that I've used in the past that works pretty well. The responses aren't as robust as google's but it's a good start.
The api looks like so in a JSON response.
{
Abstract: ""AbstractText: ""AbstractSource: ""AbstractURL: ""Image: ""Heading: ""Answer: ""Redirect: ""AnswerType: ""Definition: ""DefinitionSource: ""DefinitionURL: ""RelatedTopics: [ ]Results: [ ]Type: ""
}
I hope this helps!
Solution 2:
I've done a lot of research and it seems like there isn't anything currently available like you've described. There isn't anything that could pull information from Google Searches either.
The only thing I could think of that could be an alternative is getting information via RSS (http://www.w3schools.com/xml/xml_rss.asp) and implementing that in a program somehow.
Solution 3:
A bit late, but here is a working solution in 2017 that uses Python and Selenium (with the headless chromedriver) to extract the "primary" text from the answer box, based on the fact that the formatting of the search page and answer box is reasonably consistent across different types of queries (though I haven't tested this exhaustively). Of course, the element coordinates may change depending on resolution/window size, but adjusting for that is easy enough.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--window-size=1024x768")
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
defask_google(query):
# Search for query
query = query.replace(' ', '+')
driver.get('http://www.google.com/search?q=' + query)
# Get text from Google answer box
answer = driver.execute_script(
"return document.elementFromPoint(arguments[0], arguments[1]);",
350, 230).text
return answer
And testing this approach with your queries (or close to them) produces:
ask_google("what is the time in Japan")
"4:36 PM"
ask_google("where is tokyo located in japan")
"Situated on the Kanto Plain, Tokyo is one of three large cities, the other two being Yokohama and Kawasaki, located along the northwestern shore of Tokyo Bay, an inlet of the Pacific Ocean on east-central Honshu, the largest of the islands of Japan."
Solution 4:
SerpApi supports direct answer box. It seems to support time as well:
$ curl https://serpapi.com/search.json?q=time+in+japan
...
"answer_box": {
"type": "local_time",
"result": "4:37 AM"
},
....
Some documentation: https://serpapi.com/direct-answer-box-api
Solution 5:
I have created a function which scrapes google client side to get the answers from the quick answer box in google. Obviously it's not perfect, but it works pretty well!
asyncfunctionanswer(q) {
var html = awaitfetch(
`https://cors.explosionscratc.repl.co/google.com/search?q=${encodeURI(q)}`,
{
headers: {
"User-Agent":
"Mozilla/5.0 (X11; CrOS x86_64 13982.88.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.162 Safari/537.36",
},
}
).then((res) => res.text());
window.d = newDOMParser().parseFromString(html, "text/html");
var el =
d.querySelector("[id*='lrtl-translation-text']") ||
[...d.querySelectorAll(".kp-header [data-md]")][1] ||
//Calculator results
[...document.querySelectorAll(".kCrYT")]?.[1] ||
[...d.querySelectorAll("*")]
.filter((i) => i.innerText)
.filter((i) => i.innerText.includes("Calculator Result"))
.slice(-2)?.[0]
?.innerText?.split("\n")?.[2] ||
//Snippets
[...d.querySelectorAll("*")]
.filter((i) => i.innerText)
.filter(
(i) =>
i.innerText.includes("Featured snippet from the web") ||
i.innerText.includes("Description") ||
i.innerText.includes("Calculator result")
)
.slice(-1)?.[0]
?.parentElement.querySelector("div span") ||
//Cards (like at the side)
d.querySelector(
".card-section, [class*='__wholepage-card'] [class*='desc']"
) ||
d.querySelector(".thODed")?.querySelector("div span") ||
[...d.querySelectorAll("[data-async-token]")]?.slice(-1)?.[0] ||
d.querySelector("miniapps-card-header")?.parentElement ||
d.querySelector("#tw-target");
var text = el?.innerText?.trim();
if (text.includes("translation") && text.includes("Google Translate")) {
text = text.split("Verified")[0].trim();
}
if (
text.includes("Calculator Result") &&
text.includes("Your calculations and results")
) {
text = text
.split("them")?.[1]
.split("(function()")?.[0]
?.split("=")?.[1]
?.trim();
}
return text;
}
This scrapes the google search page, then parses HTML for answers:
awaitanswer("When were antibiotics discovered");
// "But it was not until 1928 that penicillin, the first true antibiotic, was discovered by Alexander Fleming, Professor of Bacteriology at St. Mary's Hospital in London."awaitanswer("What time is it in London");
// "4:44 PM"awaitanswer("define awesome");
//"extremely impressive or daunting; inspiring great admiration, apprehension, or fear."
document.querySelector("button").onclick = () => { answer(document.querySelector("input").value).then(console.log);
}
asyncfunctionanswer(q) {
var html = awaitfetch(
`https://cors.explosionscratc.repl.co/google.com/search?q=${encodeURI(q)}`,
{
headers: {
"User-Agent":
"Mozilla/5.0 (X11; CrOS x86_64 13982.88.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.162 Safari/537.36",
},
}
).then((res) => res.text());
window.d = newDOMParser().parseFromString(html, "text/html");
var el =
d.querySelector("[id*='lrtl-translation-text']") ||
[...d.querySelectorAll(".kp-header [data-md]")][1] ||
//Calculator results
[...document.querySelectorAll(".kCrYT")]?.[1] ||
[...d.querySelectorAll("*")]
.filter((i) => i.innerText)
.filter((i) => i.innerText.includes("Calculator Result"))
.slice(-2)?.[0]
?.innerText?.split("\n")?.[2] ||
//Snippets
[...d.querySelectorAll("*")]
.filter((i) => i.innerText)
.filter(
(i) =>
i.innerText.includes("Featured snippet from the web") ||
i.innerText.includes("Description") ||
i.innerText.includes("Calculator result")
)
.slice(-1)?.[0]
?.parentElement.querySelector("div span") ||
//Cards (like at the side)
d.querySelector(
".card-section, [class*='__wholepage-card'] [class*='desc']"
) ||
d.querySelector(".thODed")?.querySelector("div span") ||
[...d.querySelectorAll("[data-async-token]")]?.slice(-1)?.[0] ||
d.querySelector("miniapps-card-header")?.parentElement ||
d.querySelector("#tw-target");
var text = el?.innerText?.trim();
if (text.includes("translation") && text.includes("Google Translate")) {
text = text.split("Verified")[0].trim();
}
if (
text.includes("Calculator Result") &&
text.includes("Your calculations and results")
) {
text = text
.split("them")?.[1]
.split("(function()")?.[0]
?.split("=")?.[1]
?.trim();
}
return text;
}
<inputplaceholder="What do you want to search?"><button>Search!</button>
Post a Comment for "Is There An Api For The Google Answer Boxes?"