用 Python 打开 Chrome / 机械化
Opening Chrome with Python / Mechanize
我正在尝试为一款名为 travian 的在线游戏编写自动化工具。
我想使用 python 浏览器对象(使用机械库创建)登录。登录后,我想打开一个真正的网络浏览器(即 chrome)。
#here I create a mechanize browser object
b = mechanize.Browser()
#usually to login to the game you go to this
# site and put in your login and password and press submit
server = 'http://ts4.travian.com/?lang=en'
#I am trying here to use a browser object to submit the form,
#maybe there is a mistake here
b.open(server)
b.select_form("login")
b.form["name"] = 'myAccount'
b.form["password"] = 'myPassword'
b.submit()
#once mechanise has submitted the form I want to
#open the page in chrome that would usually be
#opened if I were to submit it by hand
gamePage = ("http://ts4.travian.com/dorf1.php")
webbrowser.open_new_tab(gamePage)
当我运行此代码打开登录页面时,即登录游戏的代码似乎不起作用。
你试过了吗:
b.addheaders = [('User-agent', Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3)]
在打开任何 URL 之前?
这将在 Linux 设备上模拟 Chrome。
这是我的一个老问题。实现我想要实现的目标的最简单方法是使用名为 Selenium 的 class。
我正在尝试为一款名为 travian 的在线游戏编写自动化工具。 我想使用 python 浏览器对象(使用机械库创建)登录。登录后,我想打开一个真正的网络浏览器(即 chrome)。
#here I create a mechanize browser object
b = mechanize.Browser()
#usually to login to the game you go to this
# site and put in your login and password and press submit
server = 'http://ts4.travian.com/?lang=en'
#I am trying here to use a browser object to submit the form,
#maybe there is a mistake here
b.open(server)
b.select_form("login")
b.form["name"] = 'myAccount'
b.form["password"] = 'myPassword'
b.submit()
#once mechanise has submitted the form I want to
#open the page in chrome that would usually be
#opened if I were to submit it by hand
gamePage = ("http://ts4.travian.com/dorf1.php")
webbrowser.open_new_tab(gamePage)
当我运行此代码打开登录页面时,即登录游戏的代码似乎不起作用。
你试过了吗:
b.addheaders = [('User-agent', Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3)]
在打开任何 URL 之前?
这将在 Linux 设备上模拟 Chrome。
这是我的一个老问题。实现我想要实现的目标的最简单方法是使用名为 Selenium 的 class。