Use mechanize to log into megaupload

你说的曾经没有我的故事 提交于 2019-12-04 01:58:20

问题


I am attempting to use the following code to log into megaupload. My question is, how do i that it successfully logged in? I print out the current URL at the end of the code, but when i run the script it just returns www.megaupload.com.

import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import html2text

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# The site we will navigate into, handling it's session
br.open('http://www.megaupload.com/?c=login')

# Select the first (index zero) form
br.select_form('loginfrm')

#User credentials
br.form['username'] = 'USERNAMEGOESHERE'
br.form['password'] = 'PASSWORDGOESHERE'

br.submit()


#prints out the current log in
print br.geturl()

回答1:


Search the error message in the response body:

"Username and password do not match" in br.response().read()

Or check if you got the expected cookie (simple example, tweak as needed):

any(c.domain == ".megaupload.com" and c.name == "user" for c in cj)


来源:https://stackoverflow.com/questions/4422389/use-mechanize-to-log-into-megaupload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!