我想用python登錄到網站http://ogame.us來訪問數據。在環顧網絡以瞭解如何嘗試做到這一點之後,我決定使用機械化模塊。我認爲我有代碼的一般要點,但是當我提交html表單時,什麼也沒有發生。代碼如下:使用Python的機械化提交Html表單的問題
import sys,os
import mechanize, urllib
import cookielib
from BeautifulSoup import BeautifulSoup,BeautifulStoneSoup,Tag
import datetime, time, socket
import re,sys,os,mechanize,urllib,time, urllib2
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6')]
br.open('http://ogame.us')
br.select_form(name = 'loginForm')
br['login'] = 'stackexample'
br['pass'] = 'examplepassword'
br['uni_url'] = ['uni103.ogame.us']
br.submit()
print br.geturl()
geturl()的響應與我以前的相同。任何人都知道發生了什麼事?
它似乎沒有辦法。當我打印html時,我從登錄屏幕獲取了html,geturl()仍然返回了登錄網址。 –