make me a python code that goes on google search, accepts or refuses cookies, searches for "stellar blade nikke dlc" presses enter, bypass captcha (if asked) and then return the top 10 website domains, meta title, meta descriptions and URL (as well as their position) like 1, 2 etc.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Google Search Scraper for "stellar blade nikke dlc"
Here's a Python script that uses Selenium to perform a Google search, handle cookies, bypass CAPTCHA (if possible), and extract the top 10 results:
python
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
def setup_driver():
# Configure Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Run in headless mode
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
# Initialize the Chrome driver
service = Service(executable_path='chromedriver') # Update this path if needed
driver = webdriver.Chrome(service=service, options=chrome_options)
return driver
def handle_cookies(driver):
try:
# Wait for the cookie dialog to appear (Google's EU cookie consent)
cookie_dialog = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div[role='dialog']"))
)
# Try to find and click the "Accept all" button
accept_button = cookie_dialog.find_element(By.XPATH, ".//button[contains(., 'Accept all')]")
accept_button.click()
print("Accepted cookies")
except (NoSuchElementException, TimeoutException):
print("No cookie dialog found or couldn't accept cookies")
pass
def perform_search(driver, query):
# Navigate to Google
driver.get("https://www.google.com")
# Handle cookies
handle_cookies(driver)
# Find the search box and enter the query
search_box = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "q"))
)
search_box.send_keys(query)
search_box.send_keys(Keys.RETURN)
# Wait for results to load
time.sleep(3)
# Check for CAPTCHA (this is tricky to bypass programmatically)
try:
captcha = driver.find_element(By.ID, "captcha-form")
if captcha:
print("CAPTCHA detected. Manual intervention required.")
# In a real scenario, you might need to pause and let the user solve it
# Or use a CAPTCHA solving service (not recommended for ethical reasons)
input("Please solve the CAPTCHA and press Enter to continue...")
except NoSuchElementException:
pass
def extract_results(driver):
results = []
# Find all search result elements
search_results = driver.find_elements(By.CSS_SELECTOR, "div.g")
for i, result in enumerate(search_results[:10], 1): # Get top 10 results
try:
# Extract URL
link = result.find_element(By.CSS_SELECTOR, "a")
url = link.get_attribute("href")
# Extract domain
domain = url.split('/')[2].replace('www.', '') if url else "N/A"
# Extract title
try:
title = result.find_element(By.CSS_SELECTOR, "h3").text
except NoSuchElementException:
title = "N/A"
# Extract description
try:
description = result.find_element(By.CSS_SELECTOR, "div[data-sncf]").text
except NoSuchElementException:
description = "N/A"
results.append({
"position": i,
"domain": domain,
"title": title,
"description": description,
"url": url
})
except Exception as e:
print(f"Error processing result {i}: {str(e)}")
continue
return results
def main():
search_query = "stellar blade nikke dlc"
try:
# Setup the driver
driver = setup_driver()
# Perform the search
perform_search(driver, search_query)
# Extract the results
search_results = extract_results(driver)
# Print the results
print("\nTop 10 Search Results for:", search_query)
print("="*80)
for result in search_results:
print(f"\nPosition: {result['position']}")
print(f"Domain: {result['domain']}")
print(f"Title: {result['title']}")
print(f"Description: {result['description']}")
print(f"URL: {result['url']}")
print("-"*80)
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
# Close the driver
if 'driver' in locals():
driver.quit()
if __name__ == "__main__":
main()