python mechanize get element by id and data-row for auto-posting

150 Views Asked by At

I'm trying to auto-post a form using mechanize library with python. Mechanize library uses "name" attribute. But for my project, I need to work with "id" and "data-row" together. "id" is not unique and "data-row" starts from 1 and ends at 15.

You can find the example shortened HTML below and on this link. I will click the checkboxes.

<td data-group="0" class="select">
    <label class="checkbox-label">
        <input type="checkbox" id="00" data-row="1" data-column="0" data-group="0">
        <span class="rate">%13</span>
    </label>
    <label class="checkbox-label">
        <input type="checkbox" id="01" data-row="1" data-column="1" data-group="0">
        <span class="rate">%13</span>
    </label>
    <label class="checkbox-label">
        <input type="checkbox" id="02" data-row="1" data-column="2" data-group="0">
        <span class="rate">%74</span>
    </label>
</td>
.
.
.
<td data-group="0" class="select">
    <label class="checkbox-label">
        <input type="checkbox" id="00" data-row="15" data-column="0" data-group="0">
        <span class="rate">%13</span>
    </label>
    <label class="checkbox-label">
        <input type="checkbox" id="01" data-row="15" data-column="1" data-group="0">
        <span class="rate">%16</span>
    </label>
    <label class="checkbox-label">
        <input type="checkbox" id="02" data-row="15" data-column="2" data-group="0">
        <span class="rate">%71</span>
    </label>
</td>

Python code;

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)

br.open('https://www.nesine.com/sportoto')
br.select_form(nr=1)

How can I post the data with "id" and "data-row" attributes. I also tried twill library but I stucked on same issue.

I don't want to use Selenium, this script will work on background.

0

There are 0 best solutions below