Write a headless scraping script to scrape the result in google for "Best restaurant nearby", for playwright, in all playwright supported languages. Display all code in full.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below are the headless scraping scripts for Google search results for "Best restaurant nearby" using Playwright in all supported languages: JavaScript, Python, Java, and C#.
To run this script, ensure you have Playwright installed (npm i -D playwright) and then execute it with Node.js (node google-scraper.js).
Python
python
# google_scraper.py
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
for browser_type in ["chromium", "firefox", "webkit"]:
browser = p[browser_type].launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.google.com")
page.fill('input[name="q"]', "Best restaurant nearby")
page.click('input[type="submit"]')
page.wait_for_selector('div.yuRUbf')
results = page.query_selector_all('div.yuRUbf')
result_texts = [result.inner_text() for result in results]
print(f"Results for {browser_type}:")
print(result_texts)
browser.close()
To run this script, ensure you have Playwright installed (pip install playwright and playwright install) and then execute it with Python (python google_scraper.py).
To run this script, ensure you have the Playwright Java library included in your project. If you're using Maven or Gradle, refer to the Playwright Java documentation for setup instructions.
C#
csharp
// GoogleScraper.cs
using Microsoft.Playwright;
class GoogleScraper
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
foreach (var browserType in new[] { "chromium", "firefox", "webkit" })
{
var browser = await playwright[browserType].LaunchAsync(new LaunchOptions { Headless = true });
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.GotoAsync("https://www.google.com");
await page.FillAsync("input[name=\"q\"]", "Best restaurant nearby");
await page.ClickAsync("input[type=\"submit\"]");
await page.WaitForSelectorAsync("div.yuRUbf");
var results = await page.QuerySelectorAllAsync("div.yuRUbf");
var resultTexts = new System.Collections.Generic.List<string>();
foreach (var result in results)
{
resultTexts.Add(await result.InnerTextAsync());
}
Console.WriteLine($"Results for {browserType}:");
Console.WriteLine(string.Join("\n", resultTexts));
await browser.CloseAsync();
}
}
}
To run this script, ensure you have the Playwright .NET library included in your project. Refer to the Playwright .NET documentation for setup instructions.
These scripts launch a browser in headless mode, navigate to Google, search for "Best restaurant nearby," and then scrape the result titles. The results are printed to the console for each browser type supported by Playwright.