gpt4 book ai didi

python - 使用 mechanize Python 登录 Dropbox

转载 作者:行者123 更新时间:2023-12-01 01:57:06 24 4
gpt4 key购买 nike

我正在尝试通过 Python 上的 mechanize 登录 Dropbox (dropbox.com/login)。我认为我遗漏了一些内容,因为响应返回为 HTTP Forbidden 403 错误。

这是我使用的代码:

import mechanize


url = "https://www.dropbox.com/login"
email = "<USERNAME>"
password = "<PASSWORD>"

br = mechanize.Browser()
br.set_handle_equiv(False)
br.set_handle_gzip(False)
br.set_handle_redirect(False)
br.set_handle_referer(False)
br.set_handle_robots(False)

br.addheaders = [('Host', 'www.dropbox.com')]
br.addheaders = [('Connection', 'keep-alive')]
#br.addheaders = [('Content-Length', '7385')]
br.addheaders = [('Upgrade-Insecure-Requests', 1)]
br.addheaders = [('Origin', 'https://www.dropbox.com')]
br.addheaders = [('X-Requested-With', 'XMLHttpRequest')]
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36')]
br.addheaders = [('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')]
br.addheaders = [('Content-Type', 'application/x-www-form-urlencoded;
charset=UTF-8')]
br.addheaders = [('Referer', 'https://www.dropbox.com/login?
cont=https%3A%2F%2Fwww.dropbox.com')]
br.addheaders = [('Accept-Encoding', 'gzip, deflate, br')]
br.addheaders = [('Accept-Language', 'en-US,en;q=0.9')]
br.addheaders = [('Cookie', '<COOKIE DATA>')]

response = br.open(url)
try:
formcount=0
for frm in br.forms():
if str(frm.attrs["class"])=="clearfix credentials-form login-form":
break
formcount=formcount+1
br.select_form(nr=formcount)
br.form.set_all_readonly(False)
except:
print("Unable to find login form.");
exit(1);


br.form['login_email'] = email
br.form['login_password'] = password

response = br.submit(nr = formcount)
print response.text

这是响应:

Traceback (most recent call last):
File "login.py", line 51, in <module>
response = br.submit(nr = formcount)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 685, in
submit
return self.open(self.click(*args, **kwds))
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 254, in
open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 310, in
_mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 403: Forbidden

我知道我错过了一些东西,但不知道是什么。*我会注意到,我从浏览器中拦截的请求中复制了 header 。我知道有一个适用于 Dropbox API 的 SDK,但我需要抓取该网站..

提前致谢,阿米特。

最佳答案

对 Dropbox 网站进行脚本编写/抓取违反了 the Dropbox terms 。无论如何,我不建议这样做,因为它可能会在没有通知的情况下发生更改,因此您的代码可能会损坏。

Dropbox does offer an extensive API不过,它希望能够提供您正在寻找的任何功能,以及 an official Python SDK 。我建议改用它。

如果 API 无法提供您所需的内容,请告知我们,我们会将其记录为功能请求。

关于python - 使用 mechanize Python 登录 Dropbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50042931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com