Spaces:
Sleeping
Sleeping
Commit ·
b3a0ff9
0
Parent(s):
feat: activate autonomous scraper
Browse files- scraper.py +68 -0
scraper.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from playwright.async_api import async_playwright
|
| 3 |
+
from supabase import create_client, Client
|
| 4 |
+
|
| 5 |
+
# --- 1. SETUP YOUR SUPABASE VAULT HERE ---
|
| 6 |
+
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
| 7 |
+
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
| 8 |
+
|
| 9 |
+
# Connect to the database
|
| 10 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 11 |
+
|
| 12 |
+
async def scrape_and_upload():
|
| 13 |
+
print("🥷 Booting up GovBridge Bot (Upload Mode)...")
|
| 14 |
+
|
| 15 |
+
async with async_playwright() as p:
|
| 16 |
+
browser = await p.chromium.launch(
|
| 17 |
+
headless=True,
|
| 18 |
+
args=["--disable-blink-features=AutomationControlled"],
|
| 19 |
+
ignore_default_args=["--enable-automation"]
|
| 20 |
+
)
|
| 21 |
+
context = await browser.new_context(
|
| 22 |
+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Apply the stealth mask to hide from the firewall
|
| 26 |
+
await context.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
|
| 27 |
+
page = await context.new_page()
|
| 28 |
+
|
| 29 |
+
print("🌐 Navigating to myscheme.gov.in...")
|
| 30 |
+
await page.goto("https://www.myscheme.gov.in/search")
|
| 31 |
+
await page.wait_for_timeout(5000)
|
| 32 |
+
|
| 33 |
+
print("🎯 Target acquired! Ripping the text...")
|
| 34 |
+
titles = await page.locator("h2").all_inner_texts()
|
| 35 |
+
schemes = [t.strip() for t in titles if len(t.strip()) > 5 and "Filter By" not in t]
|
| 36 |
+
|
| 37 |
+
if len(schemes) >= 2:
|
| 38 |
+
# We noticed the website groups the Title first, and the Ministry second!
|
| 39 |
+
first_scheme_title = schemes[0]
|
| 40 |
+
first_scheme_ministry = schemes[1]
|
| 41 |
+
|
| 42 |
+
print(f"\n--- INITIATING DATABASE UPLOAD ---")
|
| 43 |
+
print(f"📤 Uploading: {first_scheme_title}...")
|
| 44 |
+
|
| 45 |
+
# Package the data to match our official_documents table perfectly
|
| 46 |
+
data_to_save = {
|
| 47 |
+
"title": first_scheme_title,
|
| 48 |
+
"department": first_scheme_ministry,
|
| 49 |
+
"document_type": "Auto-Scraped Scheme",
|
| 50 |
+
"file_url": "https://www.myscheme.gov.in/search",
|
| 51 |
+
"authentic_source_url": "https://www.myscheme.gov.in"
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# Fire the data directly into your Supabase vault!
|
| 56 |
+
supabase.table("official_documents").insert(data_to_save).execute()
|
| 57 |
+
print("✅ SUCCESS! The data bypassed the terminal and is now live in your database!")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"❌ Upload failed: {e}")
|
| 60 |
+
|
| 61 |
+
else:
|
| 62 |
+
print("❌ Didn't find enough data on the page to upload.")
|
| 63 |
+
|
| 64 |
+
print("\nShutting down bot.")
|
| 65 |
+
await browser.close()
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
asyncio.run(scrape_and_upload())
|