是否有Google Google Play Box的API?

时间:2015-08-03 22:53:20

标签: html google-api

Google答案框(有时称为精选片段,知识卡或实时结果)非常有用。我想提取信息并在我自己的程序中使用它。看看HTML代码,从那里拉出它并不是那么直截了当。我做了很多研究,但我似乎无法找到任何支持。有没有人知道是否有API(或Web Search API的一部分),您可以从中检索从答案框返回的信息?

我在这里看到了答案: google api for glorious info box? ,但所提出的解决方案在上个月被弃用。

enter image description here

举个例子,这是&#34的HTML代码;在日本的时间是什么":

<!--m--><div data-hveid="30">      
<div class="vk_c vk_gy vk_sh card-section _MZc">  
<div class="vk_bk vk_ans">6:37 AM</div> 
<div class="vk_gy vk_sh"> Tuesday, <span class="_Hq">August 4, 2015</span>  
<span class="_Hq"> (GMT+9) </span>  
</div> <span class="vk_gy vk_sh">  Time in Japan  </span> 

与#34;东京所在的位置非常不同&#34;:

<!--m-->
<div class="_uX kno-fb-ctx" aria-level="3" role="heading" data-hveid="41" data-ved="0CCkQtwcoATACahUKEwiLjemg8I3HAhUTKYgKHU7jCho">
<div class="_eF" data-tts="answers" data-tts-text="Japan">Japan</div>
<div class="_Tfc">
</div></div>
<!--n-->
</li><li class="mod" data-md="61" style="clear:none">
<!--m-->
<div class="_oDd" data-hveid="42">
<span class="_Tgc _y9e">Tokyo consists of the southwestern part of the Kanto region, the <b>Izu Islands</b>, and the <b>Ogasawara Islands</b>. Tokyo is the capital of <b>Japan</b>, and the place where over 13 million people live, making it one of the most populous cities in the world.</span></div>

我基本上需要提取&#34; 6:37 AM&#34;来自第一个&#34;日本&#34;从第二个开始,但是执行动态字符串搜索会很困难,因为它们的格式非常不同。

4 个答案:

答案 0 :(得分:4)

DuckDuckGo有一个即时回答api,我过去使用过,效果很好。回复并不像谷歌那样强大,但它是一个良好的开端。

https://duckduckgo.com/api

在JSON响应中,api看起来像这样。

{
Abstract: ""
AbstractText: ""
AbstractSource: ""
AbstractURL: ""
Image: ""
Heading: ""
Answer: ""
Redirect: ""
AnswerType: ""
Definition: ""
DefinitionSource: ""
DefinitionURL: ""
RelatedTopics: [ ]
Results: [ ]
Type: ""
}

我希望这有帮助!

答案 1 :(得分:3)

我做了很多研究,似乎没有像你所描述的那样现有的东西。没有任何东西可以从Google搜索中提取信息。

我能想到的唯一可能是替代方案是通过RSS(http://www.w3schools.com/xml/xml_rss.asp)获取信息并以某种方式在程序中实现。

答案 2 :(得分:1)

有点晚了,但这是2017年的一个有效的解决方案,使用Python和Selenium(带无头的chromedriver)来提取&#34;主要&#34;来自答案框的文本,基于搜索页面和答案框的格式在不同类型的查询中相当一致的事实(尽管我还没有详尽地测试过这一点)。当然,元素坐标可能会根据分辨率/窗口大小而改变,但调整它很容易。

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)

def ask_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

使用您的查询(或接近它们)测试此方法会产生:

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."

答案 3 :(得分:0)

SerpApi支持直接回答框。似乎也支持时间:

$ curl https://serpapi.com/search.json?q=time+in+japan

...
"answer_box": {
  "type": "local_time",
  "result": "4:37 AM"
},
....

某些文档:https://serpapi.com/direct-answer-box-api