2011-09-17 2 views
0

특정 웹 사이트로 전달되어야하는 양식 데이터를 가져 와서 제출하려고합니다. 아래는 시뮬레이션해야하는 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' : '[email protected]', 
      'password' : 'somepassword'} 

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

답변

1

숨겨진 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 값으로 파싱해야합니다. 서버는 로그인 요청에 세션 쿠키가 필요할 수도 있습니다.

저는 get/post 및 beautifulsoup을 처리하기 위해 requests 모듈을 사용하여 데이터를 구문 분석합니다.

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