2015-05-29 162 views
2

我正在嘗試編寫一個稱爲travian的在線遊戲的自動化工具。 我想使用python瀏覽器對象(使用mechanise庫創建)登錄。一旦我登錄,我想打開一個真正的瀏覽器(即。鉻)。用Python/Mechanize打開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) 

當我運行這段代碼將打開登錄頁面,也就是登錄到遊戲中的代碼似乎沒有工作過。

回答

0

你試過:

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)]

你打開任何網址前?

這將在Linux設備上模擬Chrome。