经过分析,博客园登录时采用了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"))
提交评论的链接为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
如果有折腾出来的兄弟,欢迎留言指点!