views:

35

answers:

1

Hello All

im making some script with mechanize.browser module.

one of problem is all other thing is ok, but when submit() form,it not working,

so i was found some suspicion source part.

in the html source i was found such like following.

im thinking, loginCheck(this) making problem when submit form.

but how to handle this kind of javascript function with mechanize module ,so i can

successfully submit form and can receive result?

following is websource snippet which related with loginCheck(this) javascript function.

        function init(){
        FRMLOGIN.ID.focus();
    }

    function loginCheck(f){
        if(chkNull(f.ID, "아이디를"))
            return false;

        if(chkNull(f.PWD, "패스워드를"))
            return false;

        //f.target = "ifrmLoginHidden";
        f.action = (f.SECCHK.checked) ? "https://user.buddybuddy.co.kr/Login/Login.asp" : "http://user.buddybuddy.co.kr/Login/Login.asp";
    }

i know mechanize not support javascript, so i want to make progammatically loginCheck()

function with python mechanize code.

anyone would you some help me to make this javascript function to python mechanize

translated code?

so correctly can login with website?

if so much appreciate!

    # -*- coding: cp949-*-
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


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)

# Want debugging messages?
br.set_debug_http(True)
br.set_debug_redirects(True)
br.set_debug_responses(True)

# 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://user.buddybuddy.co.kr/Login/LoginForm.asp?URL=')
html = br.response().read()
print html

br.select_form(name='FRMLOGIN')
print br.viewing_html()
br.form['ID']='psh7943'
br.form['PWD']='qkrthgus'
br.submit()

print br.response().read()

if anyone can help me ..much appreciate!!

+1  A: 

You can go through login process by hand in your browser and check (using e.g. Firebug in firefox, Developer Tools in Chrome etc.) what requests are send to the site when you hit OK button. Usually this is a POST requests with data taken from login form. Check what data are send in this request and execute your own post request with: mechanize.urlopen(URL, POST_DATA). You can extract POST_DATA (and post_url) from mechanize's form object using form.click_request_data(), but you may need to do some modifications.

Very simple example:

br.select_form(name='form_name')
br.form['login']='login'
br.form['pass']='pass'
post_url, post_data, headers =  br.form.click_request_data()
mechanize.urlopen(post_url, post_data)
Pawel Markowski