Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python mechanize.browser submit() related problem

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()
like image 692
paul Avatar asked Mar 30 '10 10:03

paul


2 Answers

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.

like image 72
bikemule Avatar answered Sep 22 '22 10:09

bikemule


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.

like image 31
alex Avatar answered Sep 23 '22 10:09

alex