Python requests可以模拟登录需要身份信息的网站吗,pythonrequests,我用python的req


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

使用python的这个库吧 selenium ,这个强大,构建自动化测试都毫无压力

如果想用浏览器自动打开的话,可以尝试一下楼上兄台的建议:

Selenium是基于webdriver的著名自动化测试工具,可以轻松搞定楼主的问题。

比如我下面这段给出的example,可以完成你的需求,模拟登录人人

# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.by import Bywd = 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

感谢阅读。

编橙之家文章,

评论关闭