gpt4 book ai didi

c# - 如何在我的游戏中搜索谷歌

转载 作者:太空宇宙 更新时间:2023-11-03 19:46:51 24 4
gpt4 key购买 nike

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

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

下面是我的代码

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

最佳答案

我不认为谷歌喜欢你以这种方式使用他们的服务 - 他们真的希望你通过他们的 Custom Search API 注册并访问这些东西。 .这是一个很好的基于 JSON 的 api,因此您不必进行任何 HTML 处理。从 Unity 中的 JSON 响应中获取答案应该非常简单。

关于c# - 如何在我的游戏中搜索谷歌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44495097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com