Python Mechanize:提交按钮不起作用没有属性'click'

时间:2016-05-28 19:51:05

标签: python mechanize

此网页上有一个表单:https://www.avanza.se/mina-sidor/kontooversikt.html

我正在尝试填写并使用此提交:

# -*- coding: utf-8 -*-
import cookielib 
import urllib2 
import mechanize 
from bs4 import BeautifulSoup
from lxml import html 
import urllib2, base64, lxml
import numpy as np
import unicodedata
import datetime
import re
import time

URL = "https://www.avanza.se/mina-sidor/kontooversikt.html"

br = mechanize.Browser() 

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

# Browser options
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)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.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')]

br.open(URL)

for f in br.forms():
    if f.attrs['class'] == 'loginForm clearFix':
        br.f = f
    print f
        break

br.f[ "j_username" ] = "user"
br.f[ "j_password" ] = "pass"

res = br.submit() 

time.sleep(5)

br.open('https://www.avanza.se/handla/aktier.html/kop/5447/abb-ltd')

#Getting the response in beautifulsoup
soup = BeautifulSoup(br.response().read(), 'lxml')
print(soup.prettify().encode("utf-8"))

我的打印件验证我的表格是否正确:

<div class="section accordion-togglebar">
  <a class="toggleBar whiteBG normalBlack noTopBorder ubuntu active" href="#" data-login-method="userCredentials">
    <img src="/jmvc/avanzabank/images/inloggningsuppgifter_ic.svg" class="loginTypeIcon" alt="Inloggningsuppgifter" title="Inloggningsuppgifter">
    Användarnamn &amp; lösenord <span class="arrow fRight"></span>
  </a>
  <div class="toggleBarContent noPaddingTop ">
    <div class="messageWrapper formLogin"></div>
    <form autocomplete="off" class="loginForm clearFix" method="POST" action="/ab/noop">
      <input placeholder="Användarnamn" type="text" name="j_username" autocapitalize="none">
      <input placeholder="Lösenord" type="password" name="j_password">
      <button class="focusBtn loginButton marginTop5px marginBottom15px errorToolTipPlacement fRight" type="submit" disabled="disabled">Logga in</button>

      <a class="marginTop4px defaultSize plcLink" href="/glomt-uppgift.html">Glömt lösenord eller användarnamn?</a>
    </form>
  </div>
</div>

然而,当我尝试提交时,我得到:

Traceback (most recent call last):
  File "scrap_mec.py", line 91, in <module>
    res = br.submit() 
  File "/usr/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 541, in submit
    return self.open(self.click(*args, **kwds))
  File "/usr/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 530, in click
    request = self.form.click(*args, **kwds)
AttributeError: 'NoneType' object has no attribute 'click'

2 个答案:

答案 0 :(得分:0)

我认为您仍然必须使用select_form方法,以便浏览器知道您要提交的表单

答案 1 :(得分:0)

代替:

res = br.submit()

您需要执行以下操作:

在click方法中,您必须将name属性的值从HTML传递到按钮,其中type =“ submit”。

request = form.click(name=<name>)  # or form.click(id=<id>)
br.open(request)