The following is an example Python code:
import requests
from bs4 import BeautifulSoup
# define search query and URL
search_query = "example search query"
url = f"https://www.google.com/search?q={search_query}"
# retrieve HTML data from search query URL
response = requests.get(url)
html_data = response.text
# parse HTML data using BeautifulSoup
soup = BeautifulSoup(html_data, 'html.parser')
# find div element with class "med" which contains message for zero results
zero_results_div = soup.find("div", {"class": "med"})
# check if message for zero results exists in HTML data
if zero_results_div and "did not match any documents" in zero_results_div.text:
print(f"No results found for: {search_query}")
else:
print(f"Results found for: {search_query}")
Note that this method may not work if Google changes its HTML structure or if it detects that your code is scraping its search results. Therefore, it's important to be respectful of Google's terms of service and to use this code for educational purposes only.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-11-16 11:00:00 +0000
Seen: 8 times
Last updated: Dec 01 '21
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?