tinymeng 2018-10-30 14:23:52 3089次浏览 3条回复 1 1 0
# coding=utf-8
import requests
from pyquery import PyQuery as pq

#安装插件
# pip3.7 install requests
# pip3.7 install PyQuery

# 登录地址
login_url = "https://www.yiichina.com/login"
# 签到地址
url2 = "https://www.yiichina.com/registration"

# 构造登陆的header头
headers = {
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
    'Cache-Control': "no-cache",
    'Connection': "keep-alive",
    'Content-Length': "250",
    'Content-Type': "application/x-www-form-urlencoded",
    'Host': "www.yiichina.com",
    'Origin': "https://www.yiichina.com",
    'Pragma': "no-cache",
    'Referer': "https://www.yiichina.com/login",
    'Upgrade-Insecure-Requests': "1",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/67.0.3396.99 Safari/537.36 "
}

# 构造签到的header头
headers2 = {
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
    'Cache-Control': "no-cache",
    'Connection': "keep-alive",
    'Content-Length': "250",
    'Content-Type': "application/x-www-form-urlencoded",
    'Host': "www.yiichina.com",
    'Origin': "https://www.yiichina.com",
    'Pragma': "no-cache",
    'Referer': "https://www.yiichina.com/",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/67.0.3396.99 Safari/537.36",
    'X-CSRF-Token': "jiZGgvngdp0HiiclI4SheJlJr5-lVyymIFgbfE1ZUw33FWv2mI5DzyrJFUFHt8tO_h3az84GH9xEHEtFCh8cfg==",
    'X-Requested-With': "XMLHttpRequest"
}


# 登陆方法和签到
def login_sign():
    r_session = requests.Session()
    # 请求页面
    page = r_session.get(login_url)
    # 请求获得的内容
    content = pq(page.content)

    # 获取csrf_token
    item1 = content('meta[name=csrf-token]').items()
    csrf = ''
    for nurl in item1:
        csrf = nurl.attr('content')

    data = {
        "_csrf": csrf,
        "LoginForm[username]": "xxxxxx",#帐号
        "LoginForm[password]": "xxxxxx",#密码
        'LoginForm[rememberMe]': "0",
        "LoginForm[rememberMe]": "1",
        "login - button": ""
    }
    # 完成用户登录
    response1 = r_session.post(login_url, data=data, headers=headers)
    # 请求获得的内容
    content = pq(response1.content)

    # 获取csrf_token
    item1 = content('meta[name=csrf-token]').items()
    csrf = ''
    for nurl in item1:
        csrf = nurl.attr('content')

    data2 = {
        "_csrf": csrf
    }
    # 模拟签到请求
    response2 = r_session.post(url2, data=data2, headers=headers2)
    content = pq(response2.content)
    items = content('p').items()
    for a in items:
        print(a.html())


if __name__ == "__main__":
    login_sign()



觉得很赞
  • 回复于 2018-10-31 09:01 举报

    python 来踢馆来了?

    1 条回复
    回复于 2018-10-31 15:19 回复

    当然没有,热衷yii2,闲的没事看见大家签到多那么多凌晨0:00的时候签到,发现好多天都是他,默默想到😄,然后自己用python写了一个,分享出来使用,不知道馆长会不会打我

    觉得很赞
  • 回复于 2018-11-02 10:50 举报

    写的有点骚啊 python 语法都忘了 收藏了

    1 条回复
    回复于 2018-11-02 18:11 回复

    哈哈 正常用户手动操作的登录逻辑写的 就用到了个py的jquery 很好理解,就为了躲避yii 的表单 csrf-token 写的有点啰嗦😄

    觉得很赞
  • 回复于 2018-11-27 15:41 举报

    您好, 想请教您一个问题!!
    使用request.session()来保持会话登录, 登陆过后, 调用的接口会提示账户登陆失败或者未登录 是什么原因呢?

    1 条回复
    回复于 2018-11-29 10:13 回复

    你确定已经登录成功了吗,还是因为你又重新实例化了request.session() 对象

您需要登录后才可以回复。登录 | 立即注册