Search notes:

python standard library: urllib.request

Simple demo

urllib.request.urlopen returns a http.client.HTTPResponse` object.
urllib.requests.urlretrieve downloads the content of a URL to a file.
import urllib.request

url = 'https://raw.githubusercontent.com/ReneNyffenegger/about-python/master/standard-library/urllib/request/demo.py'

html_text = urllib.request.urlopen(url).read()
#
# The type of html_text is 'bytes'
#

print(html_text.decode('utf-8'))

urllib.request.urlretrieve (url, 'downloaded-script.py');
Github repository about-python, path: /standard-library/urllib/request/demo.py

Exception for urlopen

urlopen raises urllib.error.URLError if something goes wrong:
try:
   res = urllib.request.urlopen('https://domain-does-not-exi.st/')
except urllib.error.URLError as e:
   print(e.reason)
urllib.error.HTTPError allows to filter for more specific errors;
try:
   res = urllib.request.urlopen('https://renenyffenegger.ch/does-not-exist')
except urllib.error.HTTPError as e:
   print(e.reason)

See also

The requests library.
standard library, especially http.client.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1788386058, '216.73.217.21', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/standard-library/urllib/request/index(77): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78