Skip to content

Instantly share code, notes, and snippets.

@jundaf
Created August 1, 2012 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jundaf/3222339 to your computer and use it in GitHub Desktop.
Save jundaf/3222339 to your computer and use it in GitHub Desktop.
A script uses requests and BeautifulSoup to login to kq.neusoft.com and punch the clock online.
#!/usr/bin/env python
"""A script uses requests and BeautifulSoup to login to
kq.neusoft.com and punch the clock online.
"""
import sys
import logging
logging.basicConfig(level=logging.ERROR)
import requests
from BeautifulSoup import BeautifulSoup
HOST = 'http://kq.neusoft.com'
USER = kitty
PASSWD = '123456'
sess = requests.session()
resp = sess.get(HOST)
soup = BeautifulSoup(resp.text)
form = soup.find('form')
if not form:
logging.error("login_form not found")
sys.exit(1)
action = form['action']
logging.debug(action)
login = {}
for xin in form.findAll('input'):
name = xin.get('name', None)
if name:
value = xin.get('value', None)
if value:
login[name] = value
continue
elif xin.has_key('id'):
login[name] = USER
continue
elif xin.get('type') == 'password':
login[name] = PASSWD
continue
else:
login[name] = ''
for item in login:
logging.debug("%s=%s", item, login[item])
resp = sess.post(HOST + action, data=login)
soup = BeautifulSoup(resp.text)
jsp = '/record.jsp'
record = soup.find('form', action=jsp)
if not record:
logging.error("record_form not found")
sys.exit(1)
employee = record.find('input')
idname, idvalue = employee['name'], employee['value']
logging.debug("%s=%s", idname, idvalue)
resp = sess.post(HOST + jsp, data={idname: idvalue})
soup = BeautifulSoup(resp.text)
for info in soup.findAll('td', attrs={'class': 'black12'}):
print info.string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment