views:

487

answers:

3

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.

<form method="post" onsubmit="return loginCheck(this)" name="FRMLOGIN"/>

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?

folloing is my current script source.

if anyone can help me ..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']='zero1zero2'
br.form['PWD']='012045'
br.submit()

print br.response().read()
+1  A: 

onsubmit is just ignored by mechanize, no javascript interpretation is done.
You need to verify what loginCheck(); in some limited case (Validation) you can do programmatically what javascript does.

systempuntoout
hello thanks...if you know some example would you some tell me? if so it good reference for me! thanks
paul
Looking at loginCheck, what does it do?
systempuntoout
Action "https://user.buddybuddy.co.kr/Login/Login.asp" is given by javascript.I'm sorry but you can't use mechanize in this case.
systempuntoout
what kind of module best do handling in this case? maybe urllib is best choice for me?
paul
can i use mechanize click function ? such like br.click
paul
No you can't.Use urllib and try to craft the correct POST to the http://user.buddybuddy.co.kr/Login/Login.asp url :)
systempuntoout
+1  A: 

hi there,

you will either need to make use of unmaintained module DOMForm and Spidermonkey (http://pypi.python.org/pypi/python-spidermonkey) to process javascript. Or you figure out what loginCheck() is doing and perform its work prior form submission in python. If loginCheck() just checks for obvious validity of login data, that should be pretty easy. Please note, that the action parameter of the stated form tag is missing. It's probably given in the javascript part.

Depending on what you intend it might be easier to work with urllib2 only. You might assume a static appearance of that web page and just post data with urllib2's methods and get the results with it also.

thanks i will try :)
paul
+1  A: 

mechanize doesn't support Javascript at all. If you absolutely have to run that Javascript, look into Selenium. It offers python bindings to control a real, running browser like Firefox or IE.

prayfomojo