python打开网页被禁止,如何禁用在python web浏览器的消息?

In my python program, when I send the user to create a gmail account by use of the webbrowser module, python displays:

"Please enter your Gmail username: Created new window in existing browser session."

Is there any way to get rid of "created new window in existing browser session", as it takes up the space where the user types in their Gmail account.

The code for this is:

webbrowser.open('https://www.google.com/accounts/NewAccount?service=mail')

gmail_user = raw_input('Please enter your Gmail username: ')

EDIT: After trying out both of Alex Martelli's suggestions, the code is: http://pastebin.com/3uu9QS4A

EDIT 2: I have decided just to tell users to go to the gmail registration page instead of actually sending them there, as that is much simpler to do and results in no (currently-unsolvable-by-me) errors.

解决方案

As S.Lott hints in a comment, you should probably do the raw_input first; however, that, per se, doesn't suppress the message from webbrowser, as you ask -- it just postpones it.

To actually suppress the message, you can temporarily redirect standard-output or standard-error -- whichever of the two your chosen browser uses to emit that message. It's probably no use to redirect them at Python level (via sys.stdout or sys.stderr), since your browser is going to be doing its output directly; rather, you can do it at the operating-system level, e.g., for standard output:

import os

gmail_user = raw_input('Please enter your Gmail username: ')

savout = os.dup(1)

os.close(1)

os.open(os.devnull, os.O_RDWR)

try:

webbrowser.open(whatever)

finally:

os.dup2(savout, 1)

(for standard error instead of standard output, use 2 instead of 1). This is pretty low-level programming, but since the webbrowser module does not give you "hooks" to control the way in which the browser gets opened, it's pretty much the only choice to (more or less) ensure suppression of that message.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值