0

我只是得到错误:

AttributeError: SecondLife instance has no attribute 'sendAMail'

怎么了?(我检查了格式,这不是错误。我检查了语法,也没有错误。)

脚本中发生的是一个 url 用 cookie 打开,我想从中获取一些信息。

import urllib2, cookielib, re
import ClientForm
import re
import smtplib

kurse = ['Entwicklung von Multimediasystemen', 'Computergrafik', 'Gestaltung von Multimediasystemen', 'Verteilte Systeme']

class SecondLife:

    def __init__(self, usernames, password):
        self.username = usernames
        self.password = password
        self.url = 'https://lsf.htw-berlin.de/qisserver/rds?state=user&type=0&application=QISPOS'

        cookiejar = cookielib.LWPCookieJar()
        cookiejar = urllib2.HTTPCookieProcessor(cookiejar)
        # debugger = urllib2.HTTPHandler(debuglevel=1)

        opener = urllib2.build_opener(cookiejar)
        urllib2.install_opener(opener)

    def sendAMail(self, smtp_server, user, password, listener, subject, text):
        smtp = smtplib.SMTP(smtp_server)
        smtp.starttls()
        smtp.login(user,password)
        msg = "SUBJECT: " + subject + "\n\n" + text
        smtp.sendmail("bln.schade@gmail.com", listener, msg)
        smtp.quit()

    def login(self):
        response = urllib2.urlopen(self.url)
        forms = ClientForm.ParseResponse(response, backwards_compat=False)

        # forms[0] is 'GET', forms[1] is 'POST'
        form = forms[0]

        try:
            form['username'] = self.username
            form['password'] = self.password
        except Exception, e:
            print 'The following error occured: \n"%s"' % e
            print
            print 'A good idea is to open a browser and see if you can log in from there.'
            print 'URL:', self.url

            exit()

        self.page = urllib2.urlopen(form.click('submit')).read()

    def friends_online(self):

        self.login()

        final = ""
        final_asi = ""
        leistungsstand = ""
        match = re.search(r"asi=\w*\d*\"", self.page)

        if match:
            final = match.group()
            final_asi = re.sub("asi=", "", final)
            final_asi = re.sub("\"", "", final_asi)

            print "vorher: " + final
            print "nachher: " + final_asi

            leistungsstand_url = "https://lsf.htw-berlin.de/qisserver/rds?state=htmlbesch&application=sospos&moduleParameter=Student&navigationPosition=functions%2Cnotenspiegel&breadcrumb=notenspiegel&topitem=functions&subitem=notenspiegel&asi=" + final_asi
            leistungsstand = urllib2.urlopen(leistungsstand_url).read()
        else:
            print "not match"



        # Ausloggen
        logout = "https://lsf.htw-berlin.de/qisserver/rds?state=user&type=4&re=last&menuid=logout&category=auth.logout"
        urllib2.urlopen(logout).read()

        website = open("lsf.html", "w")
        website.write(leistungsstand)
        website.close()

        for kurs in kurse:
            print kurs

            if (re.search(kurs, "fajfjsjj Entwicklung von Multimediasystemen hahahah")):
                self.sendAMail("smtp.googlemail.com", "user", "passw", "bln.schade@gmail.com", "kurs" , "Eine neue Note ist im LSF eingetragen.")


        #self.final_asi.replace(new, "asi=","")
        #asi[0].replace("\"","")


        #print "Final " + asi





SL = SecondLife('xyz', 'xyz')
SL.friends_online()
4

1 回答 1

3

对我有用:self.sendAMail从实例中打印出来

<bound method SecondLife.sendAMail of <__main__.SecondLife instance at 0x101d91e18>>

不过,我认为这格式问题。如果我复制并粘贴您的代码并查看空格,我会看到空格和制表符的混合使用。尤其是:

In [20]: [line for line in d if 'def' in line]
Out[20]: 
['        def __init__(self, usernames, password):\n',
 '    \tdef sendAMail(self, smtp_server, user, password, listener, subject, text):\n',
 '        def login(self):\n',
 '        def friends_online(self):\n']

\t之前def sendAMail看起来很可疑。我 75% 确定不一致的空格是导致问题的原因。尝试使用 运行您的脚本python -tt scriptname.py,这将引发关于标签使用不一致的错误。

于 2012-07-09T00:25:48.880 回答