我正在尝试使用Python登录Paychex的Time and Labor网站,以便自动跟踪我的工作时间。
到目前为止,我使用Chrome登录Paychex,查看了开发者工具“网络”标签中的各种请求,并将请求转换为Python请求格式:
import requests
import json
import sys
USER = 'username'
PASSWORD = 'password'
IMAGE_URL = 'https://landing.paychex.com/ssologin/Login.aspx/GetSecurityImage'
LOGIN_URL = 'https://landing.paychex.com/ssologin/Login.aspx/ProcessLogin'
LOGINFCC_URL = 'https://landing.paychex.com/ssologin/login.fcc'
def main():
session = requests.session()
# Get security image
data = { 'enteredUsername': USER }
session.headers.update({
'Origin': 'https://landing.paychex.com',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36',
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*',
'Referer': 'https://landing.paychex.com/ssologin/login.aspx',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
})
r = session.post(IMAGE_URL, data=json.dumps(data))
data = { 'eu': USER, 'ep': PASSWORD }
session.headers.update({
'Host': 'landing.paychex.com',
'Connection': 'keep-alive',
'Content-Length': '34',
'Accept': 'application/json, text/javascript, */*',
'Origin':' https://landing.paychex.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36',
'Content-Type': 'application/json; charset=UTF-8',
'Referer':' https://landing.paychex.com/ssologin/login.aspx',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.8',
})
r = session.post(LOGIN_URL, data=json.dumps(data))
session.headers.update({
'Host': 'landing.paychex.com',
'Connection': 'keep-alive',
'Content-Length': '653',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin': 'https://landing.paychex.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'https://landing.paychex.com/ssologin/login.aspx',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.8',
})
data = {
'__LASTFOCUS': '',
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE': '',
'__EVENTVALIDATION': '',
'SMENC': 'ISO-8859-1',
'SMLOCALE': 'US-EN',
'target': '/LandingRedirect.aspx',
'USER': USER,
'PASSWORD': PASSWORD,
}
r = session.post(LOGINFCC_URL, data=data) # Returns "Welcome USER" page. Successful login.
session.headers.update({
'Host': 'timeandlabor.paychex.com',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
})
# Should return time and labor page, but instead it hangs here until connection is reset by peer.
r = session.get('https://timeandlabor.paychex.com/secure/EmployeeHome.aspx')
print(r.content.decode('utf-8'))
if __name__ == '__main__':
main()
我理解许多标题都是多余的。第三个请求返回“欢迎,名字!”页面,所以它肯定成功登录。当我尝试使用GET(靠近底部)请求时间和人工页面时,会出现问题。它只是挂起,直到重置连接。我究竟做错了什么?
堆栈追踪:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 319, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 493, in urlopen
body=body, headers=headers)
File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 321, in _make_request
httplib_response = conn.getresponse()
File "/usr/local/lib/python3.4/http/client.py", line 1172, in getresponse
response.begin()
File "/usr/local/lib/python3.4/http/client.py", line 351, in begin
version, status, reason = self._read_status()
File "/usr/local/lib/python3.4/http/client.py", line 313, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/local/lib/python3.4/socket.py", line 371, in readinto
return self._sock.recv_into(b)
File "/usr/local/lib/python3.4/ssl.py", line 746, in recv_into
return self.read(nbytes, buffer)
File "/usr/local/lib/python3.4/ssl.py", line 618, in read
v = self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 104] Connection reset by peer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/requests/adapters.py", line 327, in send
timeout=timeout
File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 543, in urlopen
raise MaxRetryError(self, url, e)
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='timeandlabor.paychex.com', port=443): Max retries exceeded with url: /secure/EmployeeHome.aspx (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "paychex.py", line 89, in <module>
main()
File "paychex.py", line 84, in main
r = session.get('https://timeandlabor.paychex.com/secure/EmployeeHome.aspx')
File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 468, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.4/site-packages/requests/adapters.py", line 375, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='timeandlabor.paychex.com', port=443): Max retries exceeded with url:
/secure/EmployeeHome.aspx (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)
使用Python 3.4.2。