urlopen()
returns an object that acts just like a file, and supports .readlines()
:
response = urllib.request.urlopen(someAddress)
lines = response.readlines():
It also supports iteration:
response = urllib.request.urlopen(someAddress)
for line in response:
You already called .read()
on the response object; you could also just call str.splitlines()
on that:
lines = data.splitlines(True)
where True
tells str.splitlines()
to preserve line endings.