Python emulation Browser Login

Source: Internet
Author: User
Tags print print

#! /usr/bin/Env python#-*-coding:utf-8-*-import urllibimport urllib2import cookielibclassnetrobot:def __init__ (self, BaseURL): SELF.CJ=Cookielib. Cookiejar () Self.baseurl=BaseURL Self.opener=Urllib2.build_opener (urllib2. Httpcookieprocessor (SELF.CJ)) Urllib2.install_opener (Self.opener) def getData (Self,username,password,**data): Print'Origin data:', Data Self.opener.open (Self.baseurl)Try: data['Csrfmiddlewaretoken'] = [X.value forXinchSelf.cjif "Csrftoken"==x.name][0] # Except:pass data[when using Django to open CSRF middleware'username']=username data['Password']=PasswordreturnData def login (Self,follow_up_url, Username,password,**others): Print print'-'* -,'Login ...','-'* -Login_url= Self.baseurl +Follow_up_url Data= Self.getdata (username, password, * *others) Post_data=urllib.urlencode (data) Print Post_data header= {'user-agent':'mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0)'} # Pretend to be a browser request=Urllib2. Request (Login_url, Post_data, header) # Constructs a POST requestTry: Self.opener.open (Request) # Submit requests Print'^'* -,'Login Success','^'* -Except:print'!'* -,'Login Failure','!'* -Print def visit (self, follow_up_url): Request= Urllib2. Request (Self.baseurl +follow_up_url) Content=Self.opener.open (Request) print content.read ()if__name__ = ='__main__': Nr= Netrobot ('http://192.168.1.100') #指定网站根路径 Nr.login ('/login/','User',' Password', hidrem="') #此处假设需要额外的的hiRem参数 Nr.visit ('/api/folders/15/?format=json')

Python emulation Browser Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.