2

I am working with RoboBrowser on Python and I am trying to fill up a form on a website (here). Now the thing is that the form is dynamic. If you open up the link, you'll see a row for 'Country', 'State/District', 'City' and 'Location'.

The State/District row depends on which Country you select out of the drop down menu, City depends on State/District and Location depends on City. I want to be able to extract for example 'values' of State/District for a particular value of the country. For example when I write the following code:

>>> import re
>>> from robobrowser import RoboBrowser
>>> browser = RoboBrowser()
>>> browser.open('http://http://www.kidzee.com/admissions-at-kidzee/')
>>> form = browser.get_form()
>>> form
<RoboForm siteid=, adunit=, fname=, lname=, email=, mobile=,country=,state=,
city=, location=0, 6_letters_code=, admission_submit=Submit>
>>> form['country'].options
['','1','2'] #The options presented are India and Nepal.
>>>form['country'].value = '1'
>>>form['state'].options
['']

Now what I want to do it get all the possible values for form['state'].value with form['country'].value = '1' (because the list of states depends on the value of country). How do I go about doing that?

Thanks for reading my question.

Vizag
  • 743
  • 1
  • 7
  • 30
  • 1
    So, there's jquery going behind the scenes, open the network tab and select a country you'll see how the query is send at `http://www.kidzee.com/wp-admin/admin-ajax.php?action=state` you can just send a request at that url with form data *0* or *1* and you receive the data, quick question . how are you planning to tackle the captcha? – P.hunter Jun 01 '18 at 11:11
  • 1. I am not looking to fill the form. Just obtaining the data (changing values of states) being received when I change the value of the Country. 2. I tried what you said by using `requests.post` with the url in your comment and `data: 1`. What it returned is this:``. – Vizag Jun 01 '18 at 13:39
  • I am able to retrieve the data now. I just had to use ` 'state': '1'` in the post request as data. It shows all the states in the country now. Thanks for pointing me in the right direction. – Vizag Jun 01 '18 at 13:48

1 Answers1

1

Upon examining the network log, credits: @P.hunter, I saw that a request was being sent to http://www.kidzee.com/wp-admin/admin-ajax.php?action=state whenever I pressed the Select Country button. So I wrote the following code to fetch the data:

    from urllib.parse import urlencode
    from urllib.request import Request, urlopen

    url = 'http://www.kidzee.com/wp-admin/admin-ajax.php?action=state'
    fields = {'state': '1'}  

    r = Request(url, urlencode(fields).encode())
    json = urlopen(r).read().decode()
    print(json)

And now I am getting the required states in the country.

Thanks for the right direction @P.hunter.

Vizag
  • 743
  • 1
  • 7
  • 30