2016-09-30 50 views
-2

我想在多个网站上提交表格。通常我不能完全知道表单名称或表单ID,但我知道要提交的输入名称。根据表格输入的名称选择正确的表格

假设有一个网站里面有几个表单。我的代码应该检查所有表单,如果其中一个表单有一个名为“birthday”的输入值,它将提交该表单。如果有多个表单,它会将它们全部提交。

我该如何做到这一点?

回答

1

你基本上可以遍历所有形式和跳过那些不包含所需的输入形式:

for form in br.forms(): 
    if not form.find_control(name="birthday"): 
     continue 
    # fill form and submit here 

更多find_control()here

+0

所以我怎么能选择的形式,因为我不知道表单名? –

+0

@RobertZunr就是我所说的,遍历所有这些并过滤。 – alecxe

+0

我在跳过没有“生日”输入的表单。但其余的呢。我如何找到提交“生日”值的表单名称 –

0

你将需要使用iterator来检查网站上的所有表格。在这种情况下,我们将使用for。但是,这并不让我们知道我们正在使用哪种形式,它只是让我们使用它。所以我们将把0(第一个表单的ID)赋给一个变量,并且在新的迭代/循环开始时我们改变表单的时候给它加1。

currentForm = 0 
for form in br.forms(): # For every form in the website 
     currentForm += 1 # Add 1 to the current form so that the script knows which form we will be working next 
     if not forms.find_control(name = "birthday"): # If the form isn't about birthday 
       continue # Go to the next iteration/loop ignoring the statements below 
     br.select_form(nr = currentForm) # Select the birthday form 
     br.form["birthday"] = "Fill this with what you want" # Writing to the form 
     br.submit() # Submit the working form 

注:x += y等于x = x + y

相关问题