Good evening,
Consider the following code:
# openSearchResults.py - Opens several search results.
import bs4
import pyinputplus as pyip
import requests
import webbrowser
inp = pyip.inputStr(prompt="What are you looking for? ")
print('Searching...')
res = requests.get('https://google.com/search?q=' + ' '.join(inp))
res.raise_for_status()
print(f"Status: {res.status_code}")
# Retrieve top search result links.
soup = bs4.BeautifulSoup(res.text, 'html.parser')
print(soup) # output the css and html (temporarly for debugging)
# Open a browser tab for each result.
linkElems = soup.select('.package-snippet')
numOpen = min(5, len(linkElems)) # numOpen = 0 here, therefore the for loop does not execute
for i in range(numOpen):
urlToOpen = 'https://google.com' + linkElems[i].get('href')
print('Opening', urlToOpen)
webbrowser.open(urlToOpen)
What is it: This program attempts to solve the task from Al Sweigart's book "Automate the Boring Stuff with Python"
What does it do: The program intends to take input from the user, perform a google search and automatically open several tabs with a couple of the first results.
Problem: Printing the soup variable shows that the program runs into Before you continue to Google Search window (picture).
What I tried so far: I've attempted to use driver.find_element(By.ID, "L2AGLb").click() from selenium.webdriver.common.by import By as suggested here, however, I've got Unresolved reference 'driver' error. PyCharm automatically solves the issue by importing from lib2to3.pgen2 import driver, which replaces the error with a warning Cannot find reference 'find_element' in 'driver.pyi'.
What else can I try to bypass the Before you continue to Google Search window?