通过Python获取表单数据

0 投票
2 回答
615 浏览
提问于 2025-04-17 02:30

我想获取一些表单数据,然后把这些数据提交到一个特定的网站。下面是我需要模拟的HTML表单。我已经花了几个小时在这上面,但似乎一直没有成功。我希望这个能在Google App Engine上运行。如果有人能帮帮我就太好了。

<form method="post" action="/member/index.bv"> 
        <table cellspacing="0" cellpadding="0" border="0" width="100%"> 
            <tr> 
                <td align="left"> 
                    <h3>member login</h3><input type="hidden" name="submit" value="login" /><br /> 
                </td> 
            </tr> 
            <tr> 
                <td align="left" style="color: #8b6c46;"> 
                    email:<br /> 
                    <input type="text" name="email" style="width: 140px;" /> 
                </td> 
            </tr> 
            <tr> 
                <td align="left" style="color: #8b6c46;"> 
                    password:<br /> 
                    <input type="password" name="password" style="width: 140px;" /> 
                </td> 
            </t>
            <tr> 
                <td> 
                    <input type="image" class="formElementImageButton" src="/resources/default/images/btnLogin.gif" style="width: 46px; height: 17px;" /> 
                </td> 
            </tr> 
            <tr> 
                <td align="left"> 
                    <div style="line-height: 1.5em;"> 
                        <a href="/join/" style="color: #8b6c46; font-weight: bold; text-decoration: underline; ">join</a><br /> 
                        <a href="/member/forgot/" style="color: #8b6c46; font-weight: bold; text-decoration: underline;">forgot password?</a><input type="hidden" name="lastplace" value="%2F"><br /> 
                        having trouble logging on, <a href="/cookieProblems.bv">click here</a> for help
                    </div> 
                </td> 
            </tr> 
        </table> 
    </form>

现在我正在尝试用这段代码来访问它,但没有成功。我对这个还很陌生,可能是我哪里搞错了。

import urllib2, urllib

url = 'http://blah.com/member/index.bv'
values = {'email' : 'someemail@gmail.com',
          'password' : 'somepassword'}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

2 个回答

1

你缺少了一个隐藏的参数 submit=login。你试过这样做吗:

import urllib2, urllib

url = 'http://blah.com/member/index.bv'
values = {'submit':'login',
          'email' : 'someemail@gmail.com',
          'password' : 'somepassword'}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
2

这个登录页面是第三方网站的吗?如果是的话,处理起来可能比单纯提交表单输入要复杂一些。

举个例子,我刚刚在自己的网站上试了一下登录页面。简单的提交请求在我的情况下是行不通的,你访问的登录页面可能也会遇到同样的问题。

首先,登录表单可能有一个隐藏的csrf token值,你在提交登录请求时需要发送这个值。这意味着你得先get登录页面,然后从返回的html中提取出csrf token值。服务器可能还会要求在登录请求中包含它的会话cookie。

我使用的是requests模块来处理get和post请求,使用beautifulsoup来解析数据。

import requests                                                                                                                                                                                             
import zlib                                                                                                                                                                                                 
from BeautifulSoup import BeautifulSoup                                                                                                                                                                     

# first get the login page                                                                                                                                                                                                    
response = requests.get('https://www.site.com')                                                                                                                                                   
# if content is zipped, then you'll need to unzip it                                                                                                                                                                                 
html = zlib.decompress(response.read(), 16+zlib.MAX_WBITS)  
# parse the html for the csrf token                                                                                                                                                
soup = BeautifulSoup(html)                                                                                                                                                                                  
csrf_token = soup.find(name='input', id='csrf_token')['value']                                                                                                                                              

# now, submit the login data, including csrf token and the original cookie data                                                                                                                                          
response = requests.post('https://www.site.com/login',                                                                                                                                       
            {'csrf_token': csrf_token,                                                                                                                                                                  
             'username': 'username',                                                                                                                                                                            
             'password': 'ckrit'},                                                                                                                                                                           
             cookies=response.cookies)                                                                                                                                                                   

login_result = zlib.decompress(response.read(), 16+zlib.MAX_WBITS)                                                                                                                                                  
print login_result    

我不能确定GAE是否允许这样做,但至少这可能对你理解在特定情况下需要什么有所帮助。另外,正如Carl所提到的,如果提交输入是用来触发post请求的,你也得把它包含在内。在我的例子中,这个是不用的。

撰写回答