Python机械化说现有的控件不存在

时间:2014-04-15 16:12:28

标签: python web-scraping mechanize scrape

我正在尝试在python中搜索受密码保护的网站。我的代码如下:

import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup

br = mechanize.Browser()

cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Chrome')]

br.open('https://monitor1.returnpath.net/login.php')

for f in br.forms():
print f

br.select_form(nr=1)

br.form['email'] = 'email'
br.form['password'] = 'password'

for循环返回:

<form1 POST https://monitor1.returnpath.net/login.php application/x-www-form-urlencoded
  <TextControl(email=)>
  <PasswordControl(password=)>
  <CheckboxControl(remember=[1])>
  <SubmitControl(Submit=Sign In) (readonly)>>
<GET http://now.eloqua.com/e/f2.aspx application/x-www-form-urlencoded
  <TextControl(e=)>
  <HiddenControl(lang=NA) (readonly)>
  <HiddenControl(elqSiteID=841) (readonly)>
  <HiddenControl(elqFormName=nLRegFooter-1347904420246) (readonly)>
  <SubmitControl(<None>=Sign Me Up) (readonly)>
  <SubmitButtonControl(<None>=) (readonly)>>
<POST https://monitor1.returnpath.net/send_feedback.php application/x-www-form-urlencoded
  <HiddenControl(size=) (readonly)>
  <HiddenControl(nps=) (readonly)>
  <TextareaControl(desc=)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>>

这个错误:

  

mechanise._form.ControlNotFoundError:没有控件匹配名称&#39;电子邮件&#39;

输出表明&#39;电子邮件&#39;发现所以我不确定为什么它说没有控制匹配它​​?

1 个答案:

答案 0 :(得分:1)

零索引。尝试下面的代码:

br.select_form(nr=0)