您好,欢迎来到 猿书

Python脚本实现自动签到本站

易大师 2016-08-29 3558次浏览 1条评论 1 打赏作者 0 0
requests模块文档http://www.python-requests.org/ BeautifulSoup模块文档http://beautifulsoup.readthedocs.io/zh_CN/latest/ 具体代码: import requests from bs4 import B...

requests模块文档http://www.python-requests.org/

BeautifulSoup模块文档http://beautifulsoup.readthedocs.io/zh_CN/latest/

具体代码:

import requests
from bs4 import BeautifulSoup

base_url = 'http://www.51siyuan.cn/'
login_url = base_url + 'login'
sign_url = base_url + 'registration'
login = requests.get(login_url)
login_html = login.text
soup = BeautifulSoup(login_html, 'html.parser')
_csrf = soup.find(attrs={'name':'csrf-token'})['content']

login_data = {
    'LoginForm[username]':'username',
    'LoginForm[password]':'password',
    '_csrf':_csrf
}
# 登录
jar = login.cookies
r = requests.post(login_url, login_data, cookies = login.cookies)

# 签到
sign_data = {'_csrf':_csrf}
r.cookies['_csrf'] = login.cookies['_csrf']
# 模拟ajax
headers = {'X-Requested-With':'XMLHttpRequest'}
sign_r = requests.post(sign_url, sign_data, cookies = r.cookies, headers = headers )

if sign_r.status_code == 200:
    print('签到成功')

添加该脚本到crontab 0 0 * * * ,每天0点执行

0

1 条评论