0

所以我想从谷歌搜索中检索结果的数量。我不使用谷歌搜索 API,因为它没有给出正确的数字,我想要与谷歌搜索时网页完全相同的数字(这实际上是游戏的重点)。我尝试了 Python(没有经验)但失败了。然后我在互联网上搜索了一下,这就是我想出的:(找到了代码,但它是用 javascript 编写的,我对此也很愚蠢)编辑:这是 JavaScript r google 搜索结果计数检索的链接 显然有些家伙想要相同但在java中,所以如果有人可以“翻译”这个(最简单(合法)的方式以编程方式获取谷歌搜索结果计数?)我会很高兴

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Windows;
using File = System.IO.File;

public class SearchGoogle : MonoBehaviour
{
    public string keyword;
    private string searchwebsite;
    string fileName = "MyFile.html";

    void Start()
    {
        searchwebsite = "https://www.google.com/search?q=" + keyword;
        Debug.Log(searchwebsite);
        StartCoroutine(GetText());
        Debug.Log("Coroutine wird gecallt");
    }

    IEnumerator GetText()
    {
        UnityWebRequest www = UnityWebRequest.Get(searchwebsite);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log("Fehler:(");
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("CErfolg");
            // Show results as text
            Debug.Log(www.downloadHandler.text);

            if (File.Exists(fileName))
            {
                Debug.Log(fileName + " already exists.");
            }
            var sr = File.CreateText(fileName);
            sr.WriteLine(www.downloadHandler.text);
            sr.Close();

            //Printing it in a file to open it in my browser, not nessecary in the final build



            // Or retrieve results as binary data
            byte[] results = www.downloadHandler.data;
        }
    }
} 

但是这段代码只是返回了网站的源代码,我其实不太明白。有没有人对此有解决方案(我只想要结果的数量,没有别的)?我只是认为这是可能的,但如果不是,请纠正我。提前感谢。编辑:(html中的div被称为“result-stats”

4

0 回答 0