2

I wrote this Python script for signing in into Coursera so that I could download all the lecture videos from one of the courses I am enrolled in. But when I run the script I get this error

mechanize._mechanize.FormNotFoundError: no form matching nr 0

Here is the code of my script:

import mechanize

br=mechanize.Browser()
br.set_handle_robots(False)
br.open('https://class.coursera.org/inforisk-004/auth/auth_redirector?type=login&subtype=normal&email=')
br.select_form(nr=0) 
br.form["email"] = 'myemail'
br.form["password"] = 'mypassword'
br.submit()

Instead of

br.select_form(nr=0)

I also tried

br.select_form(predicate=lambda f: 'class' in f.attrs and 
               f.attrs['class'] == 'coursera-signin-form')

but then I get the error

mechanize._mechanize.FormNotFoundError: no form matching predicate <function <lambda> at 0x1b29758>
8
  • 1
    That form doesn't exist in the returned HTML. It's constructed with JavaScript.
    – Blender
    Jan 26, 2014 at 10:32
  • So does means I cannot use mechanize to login into Coursera? Jan 26, 2014 at 10:43
  • @Archit Verma You can login in browser, explore the cookies (with Firebug or Dev tools) and put them into your script. In this case, you don't have to login
    – Marat
    Jan 26, 2014 at 10:49
  • @ArchitVerma: You'd have to send the right POST request to do it,
    – Blender
    Jan 26, 2014 at 11:07
  • @Blender Can you please elaborate? Jan 26, 2014 at 11:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.