使用Python的Mechanize提交Html表单时遇到问题

时间:2011-08-14 15:15:27

标签: python mechanize

我正在尝试使用python登录网站http://ogame.us来访问数据。在浏览网页以了解如何尝试这样做之后,我决定使用机械化模块。我想我对代码有一般的要点,但是当我提交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()的响应与我以前的响应相同。有谁知道发生了什么事?

2 个答案:

答案 0 :(得分:2)

试试这个:

    data = br.submit()
    html=data.read()

答案 1 :(得分:1)

也许直接选择按钮?

response = br.submit(type="submit", id="loginSubmit")