import lxml.html
import requests
def parse_form(html):
    tree=lxml.html.fromstring(html)
    data={}
    for e in tree.cssselect('form input'):
        if e.get('name'):
            data[e.get('name')]=e.get('value')

    return data

def get_cookie():
    s = requests. session()
    result = s. get('http://example.webscraping.com/places/default/user/login?_next=/places/default/index')
    post_data = parse_form( result.text)
    print(s.cookies.get_dict())
    login_url='http://example.webscraping.com/places/default/user/login?_next=/places/default/index'
    post_data['email'] = '872992572@qq.com'
    post_data ['password'] = 'love9918'
    s.post(login_url,post_data)
    rs = s.post('http://example.webscraping.com/places/default/index')
    with open('login.html','w+') as f:
        f.write(rs. text)
    #print(rs. text)

if __name__=='__main__':
    get_cookie()

python 爬虫获取登录cookie