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