Skip to content

Instantly share code, notes, and snippets.

@BexTuychiev
Created December 11, 2025 10:08
Show Gist options
  • Select an option

  • Save BexTuychiev/2e72d58a81cb628b5f0358bd47569d46 to your computer and use it in GitHub Desktop.

Select an option

Save BexTuychiev/2e72d58a81cb628b5f0358bd47569d46 to your computer and use it in GitHub Desktop.
from playwright.sync_api import sync_playwright
import json
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page(viewport={"width": 1920, "height": 1080})
base_url = "https://quotes.toscrape.com/js/page/{}/"
all_quotes = []
for page_num in range(1, 11):
url = base_url.format(page_num)
page.goto(url)
page.locator(".quote").first.wait_for()
quotes = page.locator(".quote").all()
for quote in quotes:
text = quote.locator(".text").text_content().strip()
text = text.strip("\u201c\u201d")
author = quote.locator(".author").text_content().strip()
tags = [tag.text_content() for tag in quote.locator(".tag").all()]
all_quotes.append({
"text": text,
"author": author,
"tags": tags
})
print(f"Page {page_num}: scraped {len(quotes)} quotes")
browser.close()
with open("quotes_playwright.json", "w") as f:
json.dump(all_quotes, f, indent=2)
print(f"\nSaved {len(all_quotes)} quotes to quotes_playwright.json")
print("\nFirst 3 quotes:")
for i, q in enumerate(all_quotes[:3], 1):
print(f'{i}. "{q["text"][:60]}..."')
print(f' Author: {q["author"]} | Tags: {", ".join(q["tags"])}')
print(f"\nLast 3 quotes (#{len(all_quotes)-2} - #{len(all_quotes)}):")
for i, q in enumerate(all_quotes[-3:], len(all_quotes) - 2):
print(f'{i}. "{q["text"][:60]}..."')
print(f' Author: {q["author"]} | Tags: {", ".join(q["tags"])}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment