python 爬虫获取登录cookie


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()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM