Python的验证和使用的浏览器,urllib而CookieJar推出私人页面 [英] Python authenticate and launch private page using webbrowser, urllib and CookieJar

查看:228
本文介绍了Python的验证和使用的浏览器,urllib而CookieJar推出私人页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与cookiejar登录并并推出不登录页面,但只能被看作认证之后的页面。我知道机械化这是否但除了现在没有工作对我来说,我宁愿做没有它。现在我有,

I want to login with cookiejar and and launch not the login page but a page that can only be seen after authenticated. I know mechanize does that but besides not working for me now, I rather do this without it. Now I have,

import urllib, urllib2, cookielib, webbrowser
from cookielib import CookieJar

username = 'my_username'
password = 'my_password'
url = 'my_login_page'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'my_username' : username, 'my_password' : password})
opener.open(url, login_data)
page_to_launch = 'my_authenticated_url'
webbrowser.open(page_to_launch, new=1, autoraise=1)

我是不是能够登录并转储验证页面到stdout,或不承认的cookie启动登录页面,但我不能够在登录后推出我要的页面。帮助AP preciated。

I am either able to login and dump the authenticated page to stdout, or launch the login page without recognizing the cookie, but I am not able to launch the page I want to after logging in. Help appreciated.

推荐答案

您可以使用硒模块来做到这一点。它启动一个浏览器(Chrome,火狐,IE等)与加载到它的一个扩展,使您可以控制​​浏览器。

You could use the selenium module to do this. It starts a browser (chrome, Firefox, IE, etc) with an extension loaded into it that allows you to control the browser.

下面是你如何加载到饼干是:

Here's how you load cookies into it:

from selenium import webdriver
driver = webdriver.Firefox() # open the browser

# Go to the correct domain
driver.get("http://www.example.com")

# Now set the cookie. Here's one for the entire domain
# the cookie name here is 'key' and it's value is 'value'
driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})
# additional keys that can be passed in are:
# 'domain' -> String,
# 'secure' -> Boolean,
# 'expiry' -> Milliseconds since the Epoch it should expire.

# finally we visit the hidden page
driver.get('http://www.example.com/secret_page.html')

这篇关于Python的验证和使用的浏览器,urllib而CookieJar推出私人页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆