2013-04-24 76 views
0

我一直在尝试自动化需要cookie的网站登录。我在这个网站上找到了答案,并回复了它,但是我登录时遇到了问题,因为我忘记了我已经有一个帐户。我为这个双重职位道歉,但我担心我的回复不会被看到。使用Python自动化表单登录机械化

Can't automate login using python mechanize (must "activate" specific browser)

一个问题。当试图复制这个,我遇到了一个错误。

File "test5.py", line 6, in <module> 
self.br = mechanize.Browser(factory=mechanize.RobustFactory()) 
NameError: name 'self' is not defined 

我通常在Perl脚本,但一直在阅读,这个python模块会更容易,我试图完成。

这里是我的代码:

#!/usr/bin/python 
import sys 
import mechanize 
from mechanize import ParseResponse, urlopen, urljoin 

self.br = mechanize.Browser(factory=mechanize.RobustFactory()) 
self.br.add_handler(PrettifyHandler()) 
cj = cookielib.LWPCookieJar() 
cj.save('cookies.txt', ignore_discard=False, ignore_expires=False) 
self.br.set_cookiejar(cj) 

self.br.addheaders = [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), 
       ('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0)  Gecko/20100101 Firefox/16.0'), 
       ('Referer', 'https://--------------/admin/login.jsp'), 
       ('Accept-Encoding', 'gzip,deflate,sdch'), 
       ('Accept-Language', 'en-US,en;q=0.5'), 
       ] 
self.br.open('https://--------------/admin/login.jsp') 

# Select the first (index zero) form 
self.br.select_form(nr=0) 
# User credentials 
self.br.form['email'] = 'emailaddress' 
self.br.form['password'] = 'password' 
# Login 
self.br.submit() 
# Inventory 
body = self.br.response().read().split('\n') 

对我来说,它看起来像声明变量自身的问题,但我不是太熟悉足够使用Python知道,如果是这样的话。 任何想法,为什么我得到这个错误将不胜感激。

UPDATE :: 我能够通过删除自我的所有实例来超过最初的错误。现在,当我运行下面的代码,我得到这个错误:

raise FormNotFoundError("no form matching "+description) 
    mechanize._mechanize.FormNotFoundError: no form matching name 'Loginform' 

下面是代码:

!/usr/bin/python 
import sys 
import mechanize 
import cookielib 
from mechanize import ParseResponse, urlopen, urljoin, Browser 
from time import sleep 


class PrettifyHandler(mechanize.BaseHandler): 
def http_response(self, request, response): 
    if not hasattr(response, "seek"): 
     response = mechanize.response_seek_wrapper(response) 
    # only use BeautifulSoup if response is html 
    if response.info().dict.has_key('content-type') and ('html' in  response.info().dict['content-type']): 
     soup = MinimalSoup (response.get_data()) 
     response.set_data(soup.prettify()) 
    return response 


br = mechanize.Browser(factory=mechanize.RobustFactory()) 
br.add_handler(PrettifyHandler()) 
br.set_handle_robots(False) 
cj = cookielib.LWPCookieJar() 
cj.save('cookies.txt', ignore_discard=False, ignore_expires=False) 
br.set_cookiejar(cj) 
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) 
br.addheaders = [('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8'), 
       ('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0'), 
       ('Referer', 'https://****/admin/login.jsp'), 
       ('Accept-Encoding', 'gzip,deflate,sdch'), 
       ('Accept-Language', 'en-US,en;q=0.5'), 
       ] 
br.open('https://****/admin/login.jsp') 

print br.response 

# Select the first (index zero) form 
br.select_form(name='Loginform') 
#br.select_form(nr=0) 
# User credentials 
br.form['email'] = '[email protected]' 
br.form['password'] = 'password!!!' 
# Login 
br.submit() 
# Inventory 
body = br.response().read().split('\n') 

回答

0

只是删除每一个self.,那么它应该工作(如果有arn't任何其他错误)。

self通常仅用于从类中的方法内引用当前对象,而不是在模块级别引用。