Python urllib2 Demo Website Login

Source: Internet
Author: User

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

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.