0

I am trying to log in to booking.com to download my rental income data. The form has the html tag:

<form class="nw-signin" novalidate="">

Python code:

import mechanize
from bs4 import BeautifulSoup
import urllib2 
import cookielib

cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_cookiejar(cj)
br.open("https://account.booking.com/")

formcount=0
for frm in br.forms():  
  if str(frm.attrs["class"])=="nw-signin":
    break
  formcount=formcount+1
br.select_form(nr=formcount)
br.form['username'] = '1272897'
br.submit()

print br.response().read()

but I get the error:

FormNotFoundError                         Traceback (most recent call last)
<ipython-input-3-ee31ffcb0057> in <module>()
     15     break
     16   formcount=formcount+1
---> 17 br.select_form(nr=formcount)
     18 br.form['username'] = '1272897'
     19 br.submit()

C:\Users\kerss\Anaconda2\lib\site-packages\mechanize\_mechanize.pyc in select_form(self, name, predicate, nr, **attrs)
    666                     description.append('%s = %r' % (k, v))
    667             description = ", ".join(description)
--> 668             raise FormNotFoundError("no form matching " + description)
    669 
    670     def click(self, *args, **kwds):

FormNotFoundError: no form matching nr 0

I'm scratching my head a lot here! Can anyone see where I'm going wrong?

If I run

import mechanize
from bs4 import BeautifulSoup
import urllib2 
import cookielib

count = 0
cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_cookiejar(cj)
br.open("https://account.booking.com/")
for form in br.forms():
    count = count +1
    print count

nothing gets printed, indicating that

for form in br.forms():

is never true, so there are no forms on the page, but there cleary is a form on the page!

Runner Bean
  • 4,895
  • 12
  • 40
  • 60
  • 1
    This isn't an answer and I don't want to deter you from using your current method but you might want look into using their API https://developers.booking.com/api/commercial/index.html?page_url=getting-started I haven't look at in detail but it appears to publicly accessible, if not, as you appear to be a client I'm sure you can request a key should you need one. – Madison Courto May 21 '19 at 04:46
  • @MadisonCourto that API is for developers who want to link into booking.com to build price comparision sites, or other such sites that sell the inventory booking.com have. Im just a hotel owner who want to access my data – Runner Bean May 21 '19 at 04:49
  • That makes sense, sorry like I said I didn't look into it. Is this of help? https://stackoverflow.com/questions/16020117/mechanize-mechanize-formnotfounderror-no-form-matching-name-q – Madison Courto May 21 '19 at 04:51
  • Alternatively https://www.tokeet.com/index.html – Madison Courto May 21 '19 at 04:56
  • Python mechanize is dead, you might want to look into Python requests or Ruby mechanize. – pguardiario May 21 '19 at 08:33

0 Answers0