如何在我的游戏中搜索Google [英] How do I search google from within my game

查看:134
本文介绍了如何在我的游戏中搜索Google的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的游戏中使用Google搜索.我想找出特定搜索查询返回的结果数.

I am trying to use google search in my game. I'd like to find out the number of results a specific search query returns.

当前,我正在打开一个带有搜索查询的URL.但是,谷歌的工作方式是通过加载页面,然后在搜索结果中流式传输. Unity认为页面已完全加载,然后过早返回结果.

Currently I am opening a URL with a search query. However the way google works is by loading the page and then streaming in the search results. Unity believes the page has fully loaded and then returns the results too soon.

下面是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;

public class SearchWeb: MonoBehaviour {

  // I call this method to start the search from an input field
  public void Search(InputField _inputField) {
    StartCoroutine(GetHtml(_inputField.text.ToString(), ShowResults));
  }

  // This is my callBack that will post results in the console
  public void ShowResults(int _results, string _searchQuery) {
    Debug.Log("searching for " + _searchQuery + " results in " + _results + " results.");
  }

  // This method is responsible for recieving the HTML from a search query
  // 
  IEnumerator GetHtml(string _searchQuery, Action < int, string > _callback) {
    // White space in the a google search will return an error from google
    _searchQuery = _searchQuery.Replace(" ", "+");
    // The URL to send
    string url = "https://www.google.com/search?sclient=psy-ab&btnG=Search&q=" + _searchQuery.ToString();
    WWW wwwHTML = new WWW(url);
    Debug.Log("Attempting to search " + url);
    yield
    return wwwHTML;
    // Process the returned HTML, get useful information out of it
    int _results = GetResults(wwwHTML.text.ToString());
    // Send it back
    _callback(_results, _searchQuery);
    yield
    return null;
  }

  // This method is incomplete but would eventually return the number of results my search query found
  private int GetResults(string _html) {
    Debug.Log(_html);
    // Here I can tell by looking in the console if the returned html includes the search results
    // At the moment it doesn't

    int _startIndex = 0;
    int _results = 0;

    // looking for this <div id="resultStats"> where the search results are placed
    if (_html.IndexOf("<div id=\"resultStats\">", System.StringComparison.Ordinal) > 0) {
      _startIndex = _html.IndexOf("<div id=\"resultStats\">", System.StringComparison.Ordinal);
      // TODO - Finish this stuff
    }
    // TODO - For now I'm just debugging to see if I've found where the result stats may be
    _results = _startIndex;
    return _results;
  }
}

当前返回不包含任何搜索结果的html

It currently returns html which doesnt contain any search results

推荐答案

我不认为Google像您这样使用他们的服务-他们真的希望您通过他们的自定义搜索API .这是一个很好的基于JSON的api,因此您避免进行任何HTML处理.从Unity中的JSON响应中获取答案应该很简单.

I don't think google like you using their services in this way - they'd really like you to sign up and access these kind of things via their Custom Search API. This is a nice JSON based api, so you avoid having to do any HTML processing. Getting the answers out of the JSON response in Unity should then be pretty straight forward.

这篇关于如何在我的游戏中搜索Google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆