Python使用webbrowser,urllib和CookieJar验证并启动私有页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python使用webbrowser,urllib和CookieJar验证并启动私有页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1432字,纯文字阅读大概需要3分钟。
内容图文
![Python使用webbrowser,urllib和CookieJar验证并启动私有页面](/upload/InfoBanner/zyjiaocheng/703/8fda1ebc146e4eceab8a33282c155995.jpg)
我想使用cookiejar登录,并且不启动登录页面,而是启用经过身份验证后才能看到的页面.我知道机械化这样做但除了现在不为我工作之外,我宁愿这样做也没有它.我现在有,
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的情况下启动登录页面,但我无法在登录后启动我想要的页面.帮助赞赏.
解决方法:
您可以使用selenium模块执行此操作.它启动一个浏览器(chrome,Firefox,IE等),其中加载了一个扩展程序,允许您控制浏览器.
以下是将cookie加载到其中的方法:
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使用webbrowser,urllib和CookieJar验证并启动私有页面全部内容,希望文章能够帮你解决Python使用webbrowser,urllib和CookieJar验证并启动私有页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。