我正在尝试编写脚本来登录我的大学课程网站(http://nalanda.bits-pilani.ac.in/login/),以自动下载我的所有课程文件。我正在使用请求包与服务器进行通信。它正在工作,但要花费很多时间。
这是我正在使用的代码:
import requests
import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level = logging.DEBUG)
with requests.Session() as c:
url = 'http://nalanda.bits-pilani.ac.in/login/'
USERNAME = '<my username>'
PASSWORD = '<my password>'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'}
login_data = dict(username = USERNAME, password = PASSWORD)
print('Sending authentication details...')
r = c.post(url,data = login_data)
print('Authentication done in '+str(end-start))
r.raise_for_status()
print('Getting list of courses...')
page = c.get('http://nalanda.bits-pilani.ac.in/my/')
这是输出:
Sending authentication details...
07/17/2018 07:13:29 PM Starting new HTTP connection (1): nalanda.bits-pilani.ac.in
07/17/2018 07:15:41 PM http://nalanda.bits-pilani.ac.in:80 "POST /login/ HTTP/1.1" 303 463
07/17/2018 07:15:41 PM http://nalanda.bits-pilani.ac.in:80 "GET /login/index.php?testsession=3053 HTTP/1.1" 303 434
07/17/2018 07:15:41 PM http://nalanda.bits-pilani.ac.in:80 "GET /my/ HTTP/1.1" 200 15578
Getting list of courses...
07/17/2018 07:15:42 PM http://nalanda.bits-pilani.ac.in:80 "GET /my/ HTTP/1.1" 200 15577
启动HTTP连接需要2分钟以上。之后的所有POSTS和GETS都可以正常运行。对于其他网站,例如http://www.google.com,一切正常,启动HTTP连接的时间不到一秒钟。
任何修复/解决方法将不胜感激。请注意,该网站已在我的浏览器中快速加载,并且清除了缓存。