HTTP GET
suggest changePython 3
import urllib.request
print(urllib.request.urlopen("http://stackoverflow.com/documentation/"))
# Prints: <http.client.HTTPResponse at 0x7f37a97e3b00>
response = urllib.request.urlopen("http://stackoverflow.com/documentation/")
print(response.code)
# Prints: 200
print(response.read())
# Prints: b'<!DOCTYPE html>\r\n<html>\r\n<head>\r\n\r\n<title>Documentation - Stack Overflow</title>
The module has been updated for Python 3.x, but use cases remain basically the same. urllib.request.urlopen will return a similar file-like object. A bare urlopen to that Documentation URL now raises urllib.error.HTTPError: HTTP Error 403: Forbidden. The 200 body is an old transcript. The address in the response repr changes from run to run.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents