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 - Python技术站