search up on google how to make a cake and print the results in python

To do this, we can use web scraping libraries like BeautifulSoup and requests to programmatically search Google and extract the search results.

Here's an example code snippet that demonstrates how to search Google for how to make a cake and print the result titles:

main.py
import requests
from bs4 import BeautifulSoup

query = 'how to make a cake'
url = f"https://www.google.com/search?q={query}"

response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")
search_results = soup.find_all('div', class_='BNeawe vvjwJb AP7Wnd')

for result in search_results:
    if result.parent.name == 'a':
        print(result.text)
368 chars
15 lines

This code first creates a URL for the Google search and sends a request to retrieve the HTML content. Then, it uses BeautifulSoup to parse the HTML and extract the search result titles. Finally, it prints each title to the console. Keep in mind that web scraping Google search results may be against Google's terms of service, so use this code at your own risk.

gistlibby LogSnag