0

我一直在尝试自动化需要 cookie 的站点登录。我在这个网站上找到了答案,并回复了它,但我在登录时遇到了问题,因为我忘记了我已经在这里有一个帐户。我为重复发帖道歉,但我担心我的回复不会被看到。

无法使用 python mechanize 自动登录(必须“激活”特定浏览器)

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

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')

对我来说,声明变量 self 似乎是一个问题,但我对 Python 不太熟悉,不知道是否是这种情况。任何我收到此错误的想法将不胜感激。

更新:: 我能够通过删除所有 self 实例来克服最初的错误。现在,当我运行以下代码时,出现此错误:

    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'] = 'luskbo@gmail.com'
br.form['password'] = 'password!!!'
 # Login
br.submit()
# Inventory
 body = br.response().read().split('\n')
4

1 回答 1

0

只需删除 every self.,它应该可以工作(如果没有任何其他错误)。

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

于 2013-04-24T16:17:20.647 回答