# src/run_scraper.py from playwright.async_api import async_playwright import asyncio import os import subprocess async def scrape(): """ Scrapes the top 4 post titles and URLs from Hacker News and saves them to hn.txt. """ # NOT OK, BUT NOW I DON'T HAVE ANOTHER SOLUTION. Dockerfile AND setup.sh DOESN'T HELP. # Check if the browser executable exists browser_path = os.path.expanduser('~/.cache/ms-playwright/chromium_headless_shell-1181/chrome-linux/headless_shell') if not os.path.exists(browser_path): print("Playwright browsers not found. Installing...") try: subprocess.run(["playwright", "install"], check=True) print("Playwright installation complete.") except FileNotFoundError: print("Playwright command not found. Please ensure it's in your PATH.") return # NOT OK, BUT NOW I DON'T HAVE ANOTHER SOLUTION. Dockerfile AND setup.sh DOESN'T HELP. # Use the async_playwright context manager to launch the browser async with async_playwright() as p: # Launch a browser instance (Chromium in this case) browser = await p.chromium.launch() page = await browser.new_page() await page.goto('https://news.ycombinator.com') # Use locators for a more robust way to select elements links_locator = page.locator('.titleline > a') results = [] # Iterate through the first 4 located elements for i in range(4): locator = links_locator.nth(i) title = await locator.inner_text() url = await locator.get_attribute('href') # Handle relative URLs by prepending the base URL if necessary if url and not url.startswith('http'): url = f"https://news.ycombinator.com/{url}" results.append(f"{title} - {url}") # Write the results to the output file with open('hn.txt', 'w', encoding='utf-8') as file: for result in results: file.write(result + '\n') # Always remember to close the browser await browser.close() print("Scraping complete. 'hn.txt' has been created.") if __name__ == "__main__": asyncio.run(scrape())