from urllib.request import urlopen
# Print first four lines of this site.
i = 0
for line in urlopen("http://www.example.com/"):
line = line.decode()
print(i, line, end="")
# See if past limit.
if i == 3:
i += 10 <!doctype html>
3 <title>Example Domain</title>
Parse. Web locations usually begin in http or https—these are called URLs or URIs. In Python we use the urllib.parse module to access the urlparse type.
Here Here we parse a URL. Then we access some fields from the parsed URL object result.
Next The scheme is the "http" part, with no punctuation. The netloc is the domain, with no leading or trailing punctuation.
Finally The path is the location on the domain. We use it on the root page here, so the path is a forward-slash "/."
from urllib.parse import urlparse
# Parse this url.
result = urlparse("http://www.example.com/")
# Get some values from the ParseResult.
scheme = result.scheme
loc = result.netloc
path = result.path
# Print our values.
Summary. Python can fetch external files or web pages. But the complexity of programs increases when external files are necessary—sometimes external files cause errors.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.