How to readlines() from urllib

2020-03-07 07:10发布

I have program using http and I want to read data from http:

data = urllib.request.urlopen(someAddress).read()

And prepare from it list of lines like returning lines by readlines() method for file.

How to do it?

标签: python http
2条回答
疯言疯语
2楼-- · 2020-03-07 07:15

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.

查看更多
爷、活的狠高调
3楼-- · 2020-03-07 07:23

I usually do something like. I use urllib2, but it shouldn't be so different:

from urllib2 import Request, urlopen

def getPage(link, splitting = '\n'):
    request = Request(link)
    try:
        response = urlopen(request)
    except IOError:
        return -1
    else:
        the_page = response.read()
        return the_page.split(splitting)
查看更多
登录 后发表回答