Python:mechanize找不到所有表单输入

时间:2014-04-02 17:35:36

标签: python mechanize

我需要填写姓名="电子邮件" out,但机械化说名字="电子邮件"不存在(看输出)。为什么我找不到它?我必须接受其他命令吗?或者我可以用html文件替换一些文本来解决问题吗?

  <input class="box410" type="text" VCARD_NAME="vCard.Email" id="email" name="email" value="" tabindex="17" placeholder="Kontakt E-Mail Adresse" />

代码:

import mechanize

reg = "https://reg.webmail.freenet.de/freenet/Registration"
browser = mechanize.Browser()
browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
browser.open(reg)
browser.select_form(nr = 0)
browser.form['localpart'] = "hansp3056" #Wunschname
browser.click(type="image", nr=0) #Weiter
browser.submit().read()
browser.select_form(nr = 0)
print [form for form in browser.forms()][0]

输出:

  <regForm POST https://reg.webmail.freenet.de/freenet/Registration application/x-www-form-urlencoded
  <TextControl(localpart=)>
  <RadioControl(gender=[HERR, FRAU, FIRMA])>
  <TextControl(business=)>
  <TextControl(firstname=)>
  <TextControl(lastname=)>
  <TextControl(zip=)>
  <TextControl(town=)>
  <TextControl(street=)>
  <TextControl(number=)>
  <SelectControl(bday=[*, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])>
  <SelectControl(bmonth=[*, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12])>
  <SelectControl(byear=[*])>
  <PasswordControl(password1=)>
  <PasswordControl(password2=)>
  <TextControl(code=)>
  <HiddenControl(patrick=MjQyMDY5NC1iNDkwNzU0OThlYWE5YTM5OTgyMjk3NzA1MjQ5NzU1ZS0xMzk2NDU1NDIyLTg4ZWNjMjIzZTQzMw==) (readonly)>
  <CheckboxControl(agb=[yes])>
  <ImageControl(<None>=Senden)>
  <HiddenControl(mandant=freenet) (readonly)>
  <HiddenControl(action=Registration) (readonly)>
  <HiddenControl(JSEnabled=0) (readonly)>
  <HiddenControl(productID=2001004) (readonly)>
  <HiddenControl(startDate=2014-04-02T18:17:02+02:00) (readonly)>
  <HiddenControl(orderType=MAILBASIC) (readonly)>
  <HiddenControl(referer=) (readonly)>
  <HiddenControl(mitarbeiter=0) (readonly)>
  <HiddenControl(cid=) (readonly)>
  <HiddenControl(pwdstrength=inaktiv) (readonly)>
  <HiddenControl(pwdstrength2=inaktiv) (readonly)>
  <HiddenControl(altDomain=) (readonly)>
  <HiddenControl(pidUrlValue=) (readonly)>
  <HiddenControl(epidUrlValue=) (readonly)>
  <HiddenControl(ipidUrlValue=) (readonly)>
  <HiddenControl(pcUrlValue=) (readonly)>
  <HiddenControl(subpcUrlValue=) (readonly)>
  <HiddenControl(scpacoUrlValue=) (readonly)>
  <HiddenControl(scevidUrlValue=) (readonly)>
  <HiddenControl(ccUrlValue=) (readonly)>
  <HiddenControl(pidCookieValue=) (readonly)>
  <HiddenControl(epidCookieValue=) (readonly)>
  <HiddenControl(ipidCookieValue=) (readonly)>
  <HiddenControl(scevidCookieValue=) (readonly)>
  <HiddenControl(scpacoCookieValue=) (readonly)>
  <HiddenControl(subpcCookieValue=) (readonly)>
  <HiddenControl(pcCookieValue=) (readonly)>>

代码:

browser.form["email"] = "xyz@abc.com"

输出:

Traceback (most recent call last):
  File "C:\Users\Lucas\Documents\MEGAsync_Python\Hitnews generator\email freenet.py", line 47, in <module>
    browser.form["email"] = "testmail@aol.de" #Kontaktemailadresse
  File "build\bdist.win32\egg\mechanize\_form.py", line 2780, in __setitem__
control = self.find_control(name)
  File "build\bdist.win32\egg\mechanize\_form.py", line 3101, in find_control
return self._find_control(name, type, kind, id, label, predicate, nr)
  File "build\bdist.win32\egg\mechanize\_form.py", line 3185, in _find_control
raise ControlNotFoundError("no control matching "+description)
ControlNotFoundError: no control matching name 'email'

1 个答案:

答案 0 :(得分:0)

您应首先选择要使用的表单,然后按ID指定元素。它在您引用的网页中称为localpart。这是示例代码:

import mechanize

br = mechanize.Browser()
response = br.open("https://reg.webmail.freenet.de/freenet/Registration")

# Check response here
# :
# :

form = -1
count = 0
for frm in br.forms():  
    if str(frm.attrs["id"])=="regForm":
        form = count
        break

    count += 1

# Check if form is not -1
# :
# :

br.select_form(nr=form)

或者,如果您知道只有一个表单,则可以完成br.select_form(nr=0)

然后,最后:

br.form["localpart"] = "test@test.com"