So i want to retrieve the amount of results from a google search. I dont use the Google search API because it doesnt give the right numbers, i want the exact same that are one the webpage when you google someting (thats actually the whole point of the game). I tried Python (with no experience) but failed misaberly. Then I search a bit around the Internet and this what i came up with: (found code but it was in javascript, and im also to dumb for that) Edit: Heres the link to the JavaScript r google search result count retrieve And apparently some guy wanted the same but in java, so if someone could "translate" this(easiest (legal) way to programmatically get the google search result count?) I would be really happy
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;
}
}
}
But this code just returns the scource code of the website, and I actually dont quite understand it. Does anyone have a solution for this (i just want the amount of results, nothing else)? I just think it would be possible to do, but if it's not, correct me please. Thanks in advace. Edit:(The div in html is called "result-stats"