![]() |
|
[b]"Need a simple code to check a website in Python—any recommendations?"[/b]
or
[b]"What’s the best code to check - Printable Version +- Proxy Community (https://proxycommunity.com/forum) +-- Forum: Use Case (https://proxycommunity.com/forum/forum-use-case) +--- Forum: Web Scraping (https://proxycommunity.com/forum/forum-web-scraping) +--- Thread: [b]"Need a simple code to check a website in Python—any recommendations?"[/b] or [b]"What’s the best code to check (/thread-b-need-a-simple-code-to-check-a-website-in-python%E2%80%94any-recommendations-b-%0A%0Aor-%0A%0A-b-what%E2%80%99s-the-best-code-to-check) Pages:
1
2
|
[b]"Need a simple code to check a website in Python—any recommendations?"[/b] or [b]"What’s the best code to check - deepPioneer77 - 19-01-2025 Subject: Need a simple code to check a website in Python—any recommendations? Hey everyone, I'm trying to find a reliable code to check a website python script. Basically, I just wanna see if a site is up or down. I’ve seen a few examples with `requests` or `urllib`, but not sure which one’s better. Anyone got a working snippet they’ve used before? Preferably something simple—no fancy error handling needed, just a quick status check. Thanks in advance! (Also, if you’ve got tips on making it run periodically, that’d be awesome.) “” - anonyEscapeX - 26-02-2025 Hey! For a quick code to check a website python script, I'd go with `requests`. It's super easy to use. Here's a basic snippet: ```python import requests try: response = requests.get("https://example.com", timeout=5) print("Site is up!" if response.status_code == 200 else "Site is down.") except: print("Couldn't connect.") ``` If you wanna run it periodically, just slap it in a loop with `time.sleep(60)` for checks every minute. “” - DeepTunnelSage - 09-03-2025 If you're looking for something lightweight, `urllib` works too, but `requests` is way cleaner IMO. Here's how you'd do it: ```python from urllib.request import urlopen from urllib.error import URLError try: urlopen("https://example.com", timeout=10) print("Website is reachable.") except URLError: print("Website is down or unreachable.") ``` For scheduling, check out `schedule` library or just use cron jobs if you're on Linux. “” - dataXpert77 - 17-03-2025 Honestly, if you just need a quick code to check a website python solution, you could even use `httpx`—it's like `requests` but async-friendly. ```python import httpx async def check_site(): try: async with httpx.AsyncClient() as client: r = await client.get("https://example.com") print("Up" if r.status_code == 200 else "Down") except: print("Failed to connect.") ``` Bonus: it’s faster for multiple sites! “” - anonyJumperX - 26-03-2025 For a dead-simple way, why not use `curl` via `os.system`? Not pure Python, but gets the job done: ```python import os status = os.system("curl -s -o /dev/null -w '%{http_code}' example.com") print("Site is up!" if status == 200 else "Something's wrong.") ``` Not elegant, but works in a pinch. For periodic checks, cron is your friend. “” - ShadowSeeker99 - 27-03-2025 If you're lazy like me, just use `ping` for a basic check (though it won’t catch HTTP errors): ```python import subprocess def ping_site(): try: subprocess.check_output(["ping", "-c", "1", "example.com"]) print("Site is reachable.") except: print("Site is down.") ``` Not perfect, but super fast for a quick code to check a website python script. “” - stinByte77 - 31-03-2025 Pro tip: If you're checking multiple sites, `aiohttp` is a beast for async checks. Here's a tiny example: ```python import aiohttp import asyncio async def check(url): try: async with aiohttp.ClientSession() as session: async with session.get(url) as resp: print(f"{url} is up!" if resp.status == 200 else f"{url} is down.") except: print(f"{url} failed.") asyncio.run(check("https://example.com")) ``` Might be overkill for one site, but scales like a champ. “” - shadowSurferX - 01-04-2025 For a no-frills code to check a website python script, this `requests` one-liner works: ```python print("Up" if requests.get("https://example.com").status_code == 200 else "Down") ``` But add a `timeout` unless you wanna hang forever. For scheduling, `apscheduler` is solid. “” - deepPioneer77 - 03-04-2025 Wow, thanks for all the replies! Didn’t expect so many options. Tried the `requests` snippet first, and it worked like a charm. Quick follow-up: How would I modify the code to check a website python script for multiple URLs? Like, a list of sites instead of just one? Also, cron jobs seem a bit intimidating—any Python-only alternatives for scheduling? Thanks again, y’all are legends! |