Python urllib2 Demo Website Login
1. Use the browser to log in first, and then view the Web page source, analysis login Form
2. Using Python urllib2,cookielib to impersonate a Web login
ImportUrllib,urllib2,cookielib#URLLIB2 Support Http,https
defLoginweb (site,user,pwd):" "analog Web login, login url, user name, password can not be empty login post form logic needs corresponding login site, you can use the Firefox Firebug plugin to view the login request URL and Parameters" "Formvalue={' Account': User,'Password':p WD,#Here you can add the form form according to the website } #Enable automatic cookie managementcj=Cookielib. Cookiejar () Opender=Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ))#Masquerading Browser Accessopender.addheaders=[('user-agent','mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) applewebkit/537.36 (khtml, like Gecko) chrome/37.0.2062.124 safari/537.36')] #Binding ResultsRe=Opender.open (Site,urllib.urlencode (formvalue))PrintRe.getcode ()PrintRe.read ()#here can be crawled page content, do parse, determine whether to log on the success of the logic #access to the page with cookies after successful landingPc='https://console.oray.com'#such as the Personal Center pagePcre=Opender.open (PC)PrintPcre.getcode ()PrintPcre.read ()if __name__=='__main__': Site='Https://console.oray.com/passport/login'User='uname'Password='upwd'Loginweb (site, user, password)
https://gist.github.com/kennethreitz/973705
Python urllib2 Demo Website Login