from serpapi import GoogleSearch import os SERPAPI_KEY = os.getenv("SERPAPI_KEY") def search_pdf_url(query): params = { "engine": "google", "q": query, "api_key": SERPAPI_KEY } search = GoogleSearch(params) results = search.get_dict() # Extract first PDF URL for res in results.get("organic_results", []): link = res.get("link", "") if link.lower().endswith(".pdf"): return link return None