Python uses selenium to get cookies to implement a login-free __python

Source: Internet
Author: User
Tags xpath seleniumhq


Directory installation Selenium package introduced selenium package set up Webdriver object to open the set URL and wait for response through XPath to find the login box and fill in the corresponding account password analog Click Login Verify the success of the login if the currenturl change, the login is considered successful through the object's method to obtain the current access to the Web site's session cookie after the cookie can be accessed through the URLLIB2 to the corresponding Web site and to achieve the Web crawl and other work



1. Installation of Selenium package: [HTML]View plain copy sudo pip install-u Selenium Install the PIP first if there is no PIP:[HTML]View plain copy sudo python setup.py install2. Introduction of selenium package to establish Webdriver objects: [HTML]View plain Copy from selenium import webdriver sel = Selenium.webdriver.Chrome ()
In this step, you may be prompted for the error of the Chrome path, because the operation of the Chrome browser requires a chromedriver driver to help drive the download address:
http://chromedriver.storage.googleapis.com/index.html?path=2.7/
. Download the appropriate version and extract to the directory[HTML]View Plain Copy/usr/bin
3. Open the Set URL and wait for response: [HTML]View plain Copy loginurl = ' http://weibo.com/' #open the login in page sel.get (loginurl) time.sleep (10)
4. Through XPath to find the login box, and fill in the corresponding account password, analog click Login: [HTML] View plain copy #sign  in the username   try:       sel.find_ Element_by_xpath ("//div[@id = ' pl_login_form ']/div/div[2]/div[1]/div[1]/input"). Send_keys (' yourusername ')         print  ' user success! '    except:       print  ' user error! '    time.sleep (1)    #sign  in the pasword   try:        sel.find_element_by_xpath ("//div[@id = ' pl_login_form ']/div/div[2]/div[2]/div[1]/input"). Send_ Keys (' YOURPW ')        print  ' pw success! '    except:       print  ' pw error! '    time.sleep (1)    #click  to login   try:        sel.find_element_by_xpath ("//div[@id = ' pl_login_form ']/div/div[2]/div[6]/a"). Click ()         print  ' click success! '    except:       print  ' click error! '    Time.sleep (3)   
5. Verify the success of the login, if the currenturl changes, the login is considered successful: [HTML] View Plain copy curpage_url = sel.current_url   print curpage_url   while ( Curpage_url == loginurl):        #print   ' please input the  verify code: '        print  ' please input the verify  code: '        verifycode = sys.stdin.readline ()         sel.find_element_by_xpath ("//div[@id = ' pl_login_form ']/div/div[2]/div[3]/div[1]/input"). Send_ Keys (verifycode)        try:            sel.find_element_by_xpath ("//div[@id = ' pl_login_form ']/div/div[2]/div[6]/a"). Click ()             print  ' click success! '        except:             print  ' click error! '        time.sleep (3)        curpage_url = sel.current_ url  
6. Get the session cookie for the current access site through the object method: [HTML]View Plain copy #get the session cookie = [item[' name '] + ' = ' + item[' value ' for item in Sel.get_cookies ()] #p rint cookie cookiestr = '; '. Join (item for item in cookie) print cookiestr
7. After the cookie, you can access the corresponding Web site through URLLIB2, and can achieve the Web crawl and other work: [HTML]View plain copy import urllib2 print '%%%using the URLLIB2!! ' Homeurl = sel.current_url print ' homeurl:%s '% homeurl headers = {' Cookie ': cookiestr} req = Urllib2. Request (homeurl, headers = headers) Try:response = Urllib2.urlopen (req) Text = Response.read () FD = O   Pen (' homepage ', ' W ') fd.write (text) fd.close () print ' # # #get home page HTML success!! ' Except:print ' ### get home page HTML error!! '
Reference Links:
Http://splinter.readthedocs.org/en/latest/drivers/chrome.html
http://www.testwo.com/blog/6931
http://docs.seleniumhq.org/projects/
http://docs.seleniumhq.org/docs/

Related Article

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.