编程语言
首页 > 编程语言> > python – 使用mechanize登录megaupload

python – 使用mechanize登录megaupload

作者:互联网

我试图使用以下代码登录megaupload.我的问题是,我如何成功登录?我打印出代码末尾的当前URL,但是当我运行脚本时,它只返回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()

解决方法:

搜索响应正文中的错误消息:

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

或者检查你是否得到了预期的cookie(简单的例子,根据需要进行调整):

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

标签:python,login,mechanize
来源: https://codeday.me/bug/20191009/1877349.html