5

我正在使用 robobrowser 库,我遇到了一个表单:

<form action="/results" id="search">
            <div class="s_ctr">
                <fieldset>
                    <label class="jaw" for="ln">Search by Name</label><input type="text" placeholder="Search by Name" autocomplete="off" value="" tabindex="1" name="ln" maxlength="255" class="name" id="ln" data-key="true"><span>near</span><label class="jaw" for="loc">Enter City, State</label><input type="text" placeholder="Enter City, State" autocomplete="off" value="" tabindex="2" name="loc" maxlength="255" class="location" id="loc" data-key="true">
                </fieldset>
                <input type="submit" value="Find Physician" class="orange-btn" tabindex="4" id="btn-submit">

            </div>
        </form>

我的代码:

search_form = browser.get_form(id='search')
search_form        
search_form.fields['1']= name
search_form.fields['2']= address
# # Submit the form
browser.submit_form(search_form)
browser

我收到以下错误和回溯:

Traceback:
File "C:\r1\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\r1\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "C:\r1\mlist\ml1\views.py" in ph
  48.             p = getPhone(A.name,A.address)
File "C:\r1\mlist\ml1\views.py" in getPhone
  191.     browser.submit_form(search_form)
File "C:\r1\lib\site-packages\robobrowser\browser.py" in submit_form
  343.         payload = form.serialize(submit=submit)
File "C:\r1\lib\site-packages\robobrowser\forms\form.py" in serialize
  225.         return Payload.from_fields(include_fields)
File "C:\r1\lib\site-packages\robobrowser\forms\form.py" in from_fields
  118.             payload.add(field.serialize(), field.payload_key)    

Exception Type: AttributeError at /ph/
Exception Value: 'unicode' object has no attribute 'serialize'

可以使用 robobrowser 提交此表单吗?

4

1 回答 1

4

问题在于您设置表单字段值的方式。要更改字段的值,请写入其value属性:

form.fields['ln'].value = name
# Or, as a shortcut...
form['ln'].value = name

您的示例代码实际上没有设置表单字段的值,而是将字段替换为 unicode 对象。运行form['ln'] = name不会将该ln字段的值设置为name; 它删除ln字段并将其替换为name字符串。然后,当表单尝试序列化其内容时,它会尝试调用其serialize每个字段的方法。但是字符串没有定义该方法,这会导致您遇到的回溯。

于 2014-08-02T01:41:40.267 回答