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