python - 使用 Python Mechanize 自动登录表单时遇到问题

标签 python mechanize

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

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

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

更新::
我能够通过删除所有 self.instance 来克服初始错误。现在,当我运行以下代码时,出现此错误:
    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')

最佳答案

只需删除每个 self. ,那么它应该可以工作(如果没有任何其他错误)。
self通常仅用于从类中的方法中引用当前对象,而不是在模块级别。

关于python - 使用 Python Mechanize 自动登录表单时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197039/

相关文章:

python - 动态更改遗产树中的给定类并将其替换为另一个并初始化它

ruby - 使用 Mechanize 和 Nokogiri 抓取网页并将数据存储在 XML 文档中

选择表单时 Python Mechanize 错误

python - Python 中 4D 向量的可视化

python - 如何在 Hy 中构建 Python 模块?

python - 将数据从 Nodejs 发送到 Python

Python 的 zipfile 模块无法更新条目

ruby - 我总是在使用 Mechanize 的 UTF-8 错误中得到一个无效的字节序列

ruby - 如何在类名中使用带空格的 CSS 选择器

python - 使用 mechanize 访问经过 HTTP Basic 身份验证的网页