C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The argument is the location of the web page. We next call decode() on the line. This fixes some of the data.
Tip: The last argument to print is end="". This fixes some programs with double line breaks at the end of lines.
Python program that reads lines from Internet site
from urllib.request import urlopen
# Print first four lines of this site.
i = 0
for line in urlopen("http://www.dotnetCodex.com/"):
# Decode.
line = line.decode()
# Print.
print(i, line, end="")
# See if past limit.
if i == 3:
break
i += 1
Output
0 <!doctype html><html><head><link rel=canonical
1 href=http://www.dotnetCodex.com><link rel=stylesheet
2 href=1><title>The Dev Codes</title><meta
3 name=description
And: The path is the location on the domain. We use it on the root page here, so the path is simply a forward-slash "/."
Tip: There are more fields on the ParseResult. You can just print the ParseResult and all the fields will be printed. This helps discovery.
Python program that uses urlparse
from urllib.parse import urlparse
# Parse this url.
result = urlparse("http://en.wikipedia.org/")
# Get some values from the ParseResult.
scheme = result.scheme
loc = result.netloc
path = result.path
# Print our values.
print(scheme)
print(loc)
print(path)
Output
http
en.wikipedia.org
/
Because: External files cause errors. Sometimes they are not found. Other times they are in an invalid format.