zoukankan      html  css  js  c++  java
  • python模拟登录博客园(附:问题求教)

    经过分析,博客园登录时采用了JSEncrypt加密,因此登录请求时要用密文,涉及字段如下

    input1:用户名对应的密文

    input2:密码对应的密文

    登录请求链接https://passport.cnblogs.com/user/signin

     1 import urllib
     2 import http.cookiejar
     3 import json
     4 
     5 cookie = http.cookiejar.CookieJar()
     6 cookie = http.cookiejar.MozillaCookieJar('cookie.txt')
     7 handler = urllib.request.HTTPCookieProcessor(cookie)
     8 opener = urllib.request.build_opener(handler)
     9 jsondata = {
    10     "input1": "xxxxxxxxxxxxxxxxxxxx",
    11     "input2": "xxxxxxxxxxxxxxxxxxxx",
    12     "remember": "false"
    13 }
    14 postdata = bytes(json.dumps(jsondata), 'utf8')
    15 posturl = 'https://passport.cnblogs.com/user/signin'
    16 heads = {
    17     "Accept": "application/json, text/javascript, */*; q=0.01",
    18     "Accept-Encoding": "gzip, deflate",
    19     "Accept-Language": "zh-CN,zh;q=0.8",
    20     "Connection": "keep-alive",
    21     "Content-Type": "application/json; charset=UTF-8",
    22     "Cookie": "UM_distinctid=15ada3078c4464-03af923f0903eb-3e64430f-1fa400-15ada3078c5357; pgv_pvi=4363802624; Hm_lvt_cec7b3b7ecb170891406ea59237c9168=1491961611; __gads=ID=9acae56062d760d7:T=1493871262:S=ALNI_Mbt7tJwbA5wr1buvothVjazLbzHfg; _ga=",
    23     "Host": "passport.cnblogs.com",
    24     "Origin": "https://passport.cnblogs.com",
    25     "Referer": "https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F",
    26     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
    27     "VerificationToken": "IbayEnA2AtuRpDkB7WVJBr3d0GNPJ8TF9bKPDU-tiO1FQbFu8o3MTlDx_lliTm50c8bKGWRuM6GJJn4Pd_v19c24h5I1u9f2NrO5WZ7OUtJ0U22K0UDREpHArcliER1yPL_qC_sx4HwnHJywiudTPjnuuJez1LXMYVDzRV9PxArSh7MqvidX1xU1",
    28     "X-Requested-With": "XMLHttpRequest"
    29 }
    30 request = urllib.request.Request(posturl, postdata, headers=heads)
    31 
    32 response = opener.open(request)
    33 print(response.read().decode("utf8"))
    34 # # 将cookie持久化到文件
    35 cookie.save(ignore_discard=True, ignore_expires=True)
    36 # 查看下cookie里的信息
    37 # for item in cookie:
    38 #     print('Name = ' + item.name)
    39 #     print('Value = ' + item.value)
    40 # 登录成功才能访问管理页面
    41 response = opener.open("https://i.cnblogs.com/")
    42 print(response.read().decode("utf8"))
    43 
    44 # 开始刷评论
    45 comment = bytes(json.dumps({"blogApp": "wujf",
    46                             "postId": 6475097,
    47                             "body": "xxxxxxxxxxxx",
    48                             "parentCommentId": 0
    49                             }), 'utf8')
    50 
    51 response = opener.open(
    52     "http://www.cnblogs.com/mvc/PostComment/Add.aspx", comment)
    53 #返回信息{"Id":0,"IsSuccess":false,"Message":"无效的postId!","Data":null}    
    54 print(response.read().decode("utf8"))
    View Code

    提交评论的链接为http://www.cnblogs.com/mvc/PostComment/Add.aspx

    请求json如下

    {"blogApp": "wujf",
    "postId": 6475097,
    "body": "xxxxxxxxxxxx",
    "parentCommentId": 0
    }

    但是不知道为什么博客园会一直返回

    {"Id":0,"IsSuccess":false,"Message":"无效的postId!","Data":null}

    另:环境python3.5

    如果有折腾出来的兄弟,欢迎留言指点!

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    显示/隐藏Mac下的隐藏文件
    遍历指定文件下所有文件,删除指定后缀文件
    ssh公钥
    设置状态栏(statusbar)的样式
    Silverlight上传下载三种方法解析(三)
    Silverlight上传下载三种方式解析(二)
    Silverlight 上传下载之三种方式解析
    一个简单的存储过程代码生成器
    .net 程序发生了一个不可捕获的异常
    n取的r的组合数问题
  • 原文地址:https://www.cnblogs.com/wujf/p/7156857.html
Copyright © 2011-2022 走看看