2011-09-17 133 views
0

我在尋找需要傳遞給特定網站並提交的表單數據。下面是我需要模擬的html(僅表格)。我一直在這個工作幾個小時,但似乎無法得到任何工作。我希望這可以在Google App Engine中工作。你能幫忙的話,我會很高興。通過Python獲取表單數據

<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' : '[email protected]', 
      'password' : 'somepassword'} 

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

回答

1

你錯過隱藏提交=登錄說法。你有沒有試過:

import urllib2, urllib 

url = 'http://blah.com/member/index.bv' 
values = {'submit':'login', 
      'email' : '[email protected]', 
      'password' : 'somepassword'} 

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

我剛試過,沒有工作。我不確定index.bv文件是什麼。你認爲這與它有關嗎? – shawn

+0

我懷疑index.bv是否是它。你看過你的網頁瀏覽器的調試功能嗎?例如,Firebug可以顯示發送給服務器的內容。 –

2

這是第三方網站的登錄頁面嗎?如果是這樣,那可能比簡單發佈表單輸入更多。

例如,我剛剛在我自己的網站上使用了登錄頁面。簡單的發佈請求在我的情況下不起作用,這也可能與您正在訪問的登錄頁面相同。

對於初學者,登錄表單可能有一個隱藏的csrf token值,您在發佈登錄請求時必須發送該值。這意味着您必須首先登錄頁面get並解析生成的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將允許任何的這種與否,但至少它可能是搞清楚你可能需要你的具體情況是什麼幫助。另外,正如卡爾指出的那樣,如果使用提交輸入來觸發帖子,則必須將其包含在內。在我的特殊例子中,這不是必需的。

+0

感謝幫助的人。當我回家時我會試試這個。 – shawn