python中的机械工 - 选择没有名称的表单字段

时间:2014-12-15 14:32:37

标签: python forms mechanize-python

我遇到与Selecting an unnamed text field in a mechanize form (python)Use mechanize to submit form without control name类似的问题。

我想抓一个登录屏幕后面的网站数据。但是,我不知道如何选择没有名称的表单字段。控件如下所示:

<TextControl(<None>=)>
<PasswordControl(<None>=)>
<CheckboxControl(<None>=[on])>
<SubmitButtonControl(<None>=) (readonly)>>

通常会说<TextControl(login=)>,所以我可以使用br.form['login'] = 'mylogin'但这次我不能,因为我不知道登录字段的名称。

我能够访问表单,但由于我猜的值,无法填写TextControl或PasswordControl。我的基本代码如下:

import mechanize
from bs4 import BeautifulSoup
import urllib2 
import cookielib

cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.set_handle_robots(False)
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

url = "www.example.com"
request = urllib2.Request(url, None, hdr)
response = br.open(request)
forms =  [form for form in br.forms()][0]
br.select_form(nr=0)

我试过这样的东西:

br.form.find_control(id="id").value = "loginname"

和此:

forms[0].set_value("new value", nr=0)

这会引发mechanize._response.httperror_seek_wrapper: HTTP Error 403: ForbiddenTypeError: control name must be string-like等错误。我不知道还有什么可以尝试。请帮帮我。

1 个答案:

答案 0 :(得分:1)

根据你的代码:

url = "www.example.com"
request = urllib2.Request(url, None, hdr)
response = br.open(request)
forms =  [form for form in br.forms()][0]
br.select_form(nr=0)

接下来:

aux = 0
for f in br.form.controls:
    print f,
    print '   ---> Number: ',
    print aux
    aux = aux + 1 

结果是:

<TextControl(<None>=)>   ---> Number:  0 
<PasswordControl(<None>=)>   ---> Number:  1
<CheckboxControl(<None>=[on])>   ---> Number:  2
<SubmitButtonControl(<None>=) (readonly)>   ---> Number:  3

现在,你可以试试这个:

br.form.controls[0]._value = "loginname"
br.form.controls[1]._value = "password"

所以:

for f in br.form.controls:
    print f

结果将是:

<TextControl(<None>=loginname)>
<PasswordControl(<None>=password)>
<CheckboxControl(<None>=[on])>
<SubmitButtonControl(<None>=) (readonly)>