使用Python的Mechanize提交HTML表单的问题
我正在尝试用Python登录网站 http://ogame.us,目的是获取一些数据。在网上查了一圈后,我决定使用mechanize这个模块。我觉得我大致明白代码的意思了,但当我提交网页表单时,什么也没有发生。以下是我的代码:
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()后,返回的还是我之前所在的那个网址。有人知道这是怎么回事吗?
3 个回答
1
也许可以直接选择这个按钮?
response = br.submit(type="submit", id="loginSubmit")
2
试试这个:
data = br.submit()
html=data.read()
0
我之前没有填写一个叫做(uni)的第三个字段,其他的都没问题。
以后在使用谷歌浏览器(还有其他浏览器可能也可以)时,你可以通过打开Chrome开发者工具,然后查看网络部分,来查看实际发送到浏览器的请求。这可以节省很多时间。