search

Home  >  Q&A  >  body text

requests - python实现自动登录需要用户名和密码的网站

我用python的requests的库实现了模拟登录人人,但是如何用浏览器打开这个自动登录后的网页呢?webbrowser打开session返回的url不管用......初入python,求教!

大家讲道理大家讲道理2769 days ago656

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:23:21

    If you want to open it automatically with a browser, you can try the suggestion from the guy upstairs:

    Selenium is a famous automated testing tool based on webdriver, which can easily solve the poster's problem.

    For example, the example I gave below can fulfill your needs and simulate logging in to Renren

    # -*- coding:utf-8 -*-
    
    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.by import By
    
    wd = webdriver.Firefox()
    wd.get("http://www.renren.com")
    wd.maximize_window()
    
    try:
        """这段可以查看selenium的源码,属于smart wait"""
        email = WebDriverWait(wd,timeout=10).until(EC.presence_of_element_located((By.ID,'email')),message=u'元素加载超时!')
        email.send_keys("*** 你的账号 ***")
        passwd = WebDriverWait(wd,timeout=10).until(EC.presence_of_element_located((By.ID,'password')),message=u'元素加载超时!')
        passwd.send_keys("*** 你的密码 ***")
        wd.find_element_by_id("login").click() #点击登录
    except NoSuchElementException as e:
        print e.message
    

    Thanks for reading.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:23:21

    Use selenium, the library of python, which is powerful and makes building automated tests stress-free

    reply
    0
  • Cancelreply