使用mechanize提交表单数据

时间:2014-06-28 05:56:52

标签: python mechanize

访问网址后,我收到了很多表格。我想选择bidder123456reasonseller的表单,然后提交表单。

这是表格列表

-----Start0----

<GET http://auctions.search.yahoo.co.jp/search application/x-www-form-urlencoded
  <HiddenControl(auccat=) (readonly)>
  <TextControl(p=)>
  <SubmitControl(<None>=検 索) (readonly)>
  <HiddenControl(tab_ex=commerce) (readonly)>
  <HiddenControl(ei=UTF-8) (readonly)>
  <HiddenControl(fr=auc_item) (readonly)>>
-----End 0----
-----Start1----
<POST http://edit9.auctions.yahoo.co.jp/jp/config/amgr application/x-www-form-urlencoded
  <HiddenControl(aID=k179213060) (readonly)>
  <HiddenControl(bidder=123456) (readonly)>
  <HiddenControl(action=rmwinner) (readonly)>
  <HiddenControl(cc=jp) (readonly)>
  <HiddenControl(hasRunnerUp=1) (readonly)>
  <RadioControl(reason=[seller, winner])>
  <SubmitControl(confirm=蜑企勁) (readonly)>
  <HiddenControl(advance=on) (readonly)>>
-----End 1----
-----Start2----
<POST http://edit9.auctions.yahoo.co.jp/jp/config/amgr application/x-www-form-urlencoded
  <HiddenControl(aID=k179213060) (readonly)>
  <HiddenControl(bidder=xxx) (readonly)>
  <HiddenControl(action=rmwinner) (readonly)>
  <HiddenControl(cc=jp) (readonly)>
  <HiddenControl(hasRunnerUp=1) (readonly)>
  <RadioControl(reason=[seller, winner])>
  <SubmitControl(confirm=蜑企勁) (readonly)>
  <HiddenControl(advance=on) (readonly)>>
-----End 2----
-----Start3----
<POST http://edit9.auctions.yahoo.co.jp/jp/config/amgr application/x-www-form-urlencoded
  <HiddenControl(aID=k179213060) (readonly)>
  <HiddenControl(bidder=xxx) (readonly)>
  <HiddenControl(action=rmwinner) (readonly)>
  <HiddenControl(cc=jp) (readonly)>
  <HiddenControl(hasRunnerUp=1) (readonly)>
  <RadioControl(reason=[seller, winner])>
  <SubmitControl(confirm=蜑企勁) (readonly)>
  <HiddenControl(advance=on) (readonly)>>
-----End 3----
-----Start4----
<p GET http://search.yahoo.co.jp/search application/x-www-form-urlencoded
  <TextControl(p=)>
  <SubmitButtonControl(<None>=) (readonly)>
  <HiddenControl(ei=UTF-8) (readonly)>
  <HiddenControl(fr=usf) (readonly)>>
-----End 4----

我该怎么做?对不起,我没有自己的代码。 感谢。

1 个答案:

答案 0 :(得分:0)

.forms()返回表单生成器,因此您需要使用try-except块检查每个表单。

for form in browser.forms():
  try:
    if form.get_value('bidder') == '123456':
      form['reason'] = ['seller']
      form.submit()
      break
  except ControlNotFoundError:
    pass