gpt4 book ai didi

python - 使用 Python 登录 Coursera 帐户

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:39 25 4
gpt4 key购买 nike

我从 MOOC 中学到了很多东西,所以我想为此返回一些东西我正在考虑在 kivy 中设计一个小应用程序,因此需要 python 实现,实际上我想要实现的是记录通过程序进入我的 Coursera 帐户并收集有关我目前正在学习的类(class)的信息,首先我必须登录 coursera(https://accounts.coursera.org/signin?post_redirect=https%3A%2F%2Fwww.coursera.org%2F),在搜索 Web 时我遇到了这段代码:

import urllib2, cookielib, urllib

username = "abcdef@abcdef.com"
password = "uvwxyz"

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password})
info = opener.open("https://accounts.coursera.org/signin",login_data)
for line in info:
print line

还有一些类似的代码,但没有一个对我有用,每种方法都会导致我出现这种类型的错误:

Traceback (most recent call last):
File "C:\Python27\Practice\web programming\coursera login.py", line 9, in <module>
info = opener.open("https://accounts.coursera.org/signin",login_data)
File "C:\Python27\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found

错误是由于 https 协议(protocol)还是我遗漏了什么?

我不想使用任何第 3 方库。

最佳答案

为此,我正在使用 requests,我认为它是一个很棒的 python 库。以下是它如何工作的一些示例代码:

import requests
from requests.auth import HTTPBasicAuth

credentials = HTTPBasicAuth('username', 'password')
response = requests.get("https://accounts.coursera.org/signin", auth=credentials)
print response.status_code
# if everything was fine then it prints
>>> 200

这是请求的链接:

http://docs.python-requests.org/en/latest/

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

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