"喜欢readlines()"到Session对象(Python)

Mr.*_*. A 0 python python-requests

我想从一些网页中挑选几行信息.我想(或者我)打开网页,遍历各行,检查每个关键字,找到我想要的信息.

这些页面需要一个会话.

def getpage()
    home = 'website'
    exstension1 = '/input/page'
    extension2 = '/output/page'
    indexnumber = '11100'

    sess = requests.Session()
    getter = sess.get(home+extension1)
    payload = {'foo':'bar','indexnumber':indexnumber}
    getter = sess.post(home+extension2,data=payload)

    return sess
Run Code Online (Sandbox Code Playgroud)

正如我在标题中所说的那样,我需要一个.get()的readlines()方法

a.get(somePage)###Now could I put...###.readlines()
####or
a.get(somePage).text.readlines()###?
###I don't think I want the following, for performance reasons, correct me if I am wrong
F = open(someNewFile,mode='w')
F.write(a.get(somePage).text)
F.close()
F = open(thatFileIJustMade).readlines()###All that just to turn it into a File on which I can use readlines?
Run Code Online (Sandbox Code Playgroud)

谢谢

当我尝试

a.get(somePage).readlines()
Run Code Online (Sandbox Code Playgroud)

我明白了

AttributeError: Response Object Doesn't have attribute readlines
Run Code Online (Sandbox Code Playgroud)

Luk*_*asa 7

有几种方法可以做到这一点,但最多的请求方式是使用流式传输请求以及Response.iter_lines():

r = requests.get(somePage, stream=True)

for line in r.iter_lines(1024):
    # Do stuff on this line.
Run Code Online (Sandbox Code Playgroud)