Find Source Code from Website

Member #952511 0 Tallied Votes 188 Views Share

This is a program using the urllib module to find the (html/any client-side script) source code of any website which exists.
Have fun!

import urllib
sourcecode = urllib.urlopen("http://www.any-website.com").read() #Replace any-website with whatever you like
print sourcecode

Dani AI

Generated

’s snippet is a concise way to fetch the raw HTTP response, but two practical points are worth adding for future readers. First, that pattern reflects Python‑2-era usage and only returns the server’s HTML (and any inline client scripts) — it does not execute JavaScript, so pages that build content in the browser will look incomplete. Second, character encoding and HTTP error handling need explicit attention rather than assuming one default.

A modern, more robust pattern (Python 3 + requests) combines sensible headers, timeouts, error checking and best‑effort charset detection:

import requests

try:
    resp = requests.get("https://example.com",
                        headers={"User-Agent": "Mozilla/5.0"},
                        timeout=10)
    resp.raise_for_status()
    resp.encoding = resp.apparent_encoding  # best-effort charset detection
    html = resp.text
    print(html[:500])
except requests.RequestException as err:
    print("Request failed:", err)

Sites that render content client‑side still require a browser runtime to get the final DOM. For those, a headless browser (Selenium, Playwright) or calling the same XHR/JSON endpoints the page uses is the correct approach. When exact bytes matter (images, downloads), work with resp.content and decode only after reliable detection (chardet/charset‑normalizer) with a safe fallback and errors='replace'.

Practical cautions: check Content-Type headers for charset hints, reuse a Session for multiple requests, set reasonable timeouts, follow redirects, verify TLS, and respect robots.txt and site terms. was right to flag encoding — handling it explicitly avoids mangled text in international pages and makes the fetch resilient.

M.S. 53 Light Poster

if you want it to work with every site,I guess you need to consider the encoding too:

print sourcecode.decode("utf8")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.