I am trying to create a program that takes a question with three answer choices. I want it to do a google search of the question and search the results for the answer choices and see which one comes up the most. I want to also make this more accurate with googling the question and the answer and seeing which returns the most results. Does anyone know how to do this? I know there is another question out there that answers a similar question to getting the URLs provided by the search result, but I want to get the number of results from the search.
Asked
Active
Viewed 714 times
-4
-
4Please post what you have tried. – David May 06 '18 at 20:59
-
@DavidFarrugia I have tried using the import google and the search method but that only gives u the url of the results – Aryan Sawhney May 06 '18 at 22:17
-
**Post the code you wrote and explain specifically what's wrong**. Otherwise unfortunately this comes across as seemingly a *"Give me teh codez"* question, which is detested on SO and will get instantly downvoted and closed. See [this](https://meta.stackoverflow.com/questions/253137/what-close-reason-should-i-use-for-give-me-teh-codez) for why, and how to avoid that when asking a question. Actually this one was essentially a duplicate, too, which is another reason it would be closed. – smci May 08 '18 at 10:08
1 Answers
0
I haven't had time to actually test this out, but give this a shot:
from bs4 import BeautifulSoup
import requests
def getNumberOfResults(searchTerm):
response = requests.get("https://www.google.com/search?q=" + searchTerm).content
soup = BeautifulSoup(response, 'html.parser')
result = int(str(soup.find("div", {"id": "resultStats"})).split()[3].replace(",", ""))
return result
The imports have to be installed seperately as they are not part of the python standard library. You can do that with pip. If you don't know how to use pip, take a look here.
After you get each of the results, you can compare the numbers to see which one is greatest.
To get the text from each one, like you asked in the comments below, you can:
def getResultText(searchTerm):
response = requests.get("https://www.google.com/search?q=" + searchTerm).content
soup = BeautifulSoup(response, 'html.parser')
mydivs = soup.findAll("span", {"class": "st"})
results = []
for elem in mydivs:
results.append(str(elem))
return results
This will give back a list of all the preview text shown by google. It still has html elements inside of it, but you should be able to still look through it for your keywords.

azb_
- 387
- 2
- 10