POST value related question

ken highcar at gmail.com
Mon Oct 12 20:14:28 EDT 2009


hello
i have some problem to send POST value by use mechanize.
i can't write my article to my blog site.

here is full source. and what i want to do is, im posting my article
to my blog site.
thanks in advance.

    # -*- coding: cp949 -*-
    import mechanize
    import cookielib

    # Browser
    br = mechanize.Browser()

    # Cookie Jar
    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 (X11; U; Linux i686;
en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/
3.0.1')]

    # Open some site, let's pick a random one, the first that pops in
mind:
    r = br.open('http://nid.naver.com/nidlogin.login')
    html = r.read()
    #print html


    # Show the source
    #print html
    # or
    #print br.response().read()

    # Show the html title
    #print br.title()

    # Show the response headers
    #print r.info()
    # or
    #print br.response().info()

    # Show the available forms
    for f in br.forms():
        print f

    # Select the first (index zero) form
    br.select_form(nr=0)

    # Let's search
    br.form['id']='lbu142vj'
    br.form['sID']=['on']
    br.form['pw']='wbelryl'
    br.submit()
    #print br.response().read()
    r = br.open("http://www.naver.com")
    rs = r.read().decode('utf-8')
    #print rs
    r = br.open("http://blog.naver.com/PostWriteForm.nhn?
Redirect=Write&blogId=lbu142vj&widgetTypeCall=true")
    rs = r.read()
    print rs

    for f in br.forms():
        print f

    br.select_form(nr=0)
    br.form['post.category.categoryNo']="[*1]"
    br.form['post.title']='subject'
    br.form['contents.contentsValue'] = 'content'
    br.submit()



More information about the Python-list mailing list