2011-09-17 6 views
0

特定のWebサイトに渡す必要があるフォームデータを取得して送信することを検討しています。以下は、私がシミュレートする必要がある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

あなたはhidden submit = login引数がありません。試しました:

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ログインページを開き、結果のHTMLをcsrf token値として解析する必要があります。サーバーはまた、ログイン要求でセッションクッキーを要求することがあります。

私はrequestsモジュールを使用して取得/ポストとデータを解析する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が指摘しているように、送信入力が投稿をトリガするために使用されている場合は、その投稿を含める必要があります。私の特定の例では、これは必須ではありません。

+0

ヘルプマンに感謝します。私は家に帰るときにこれを試してみる。 – shawn

関連する問題