BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #18329同步于 2017/7/19
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

【更新】模拟论坛登录的代码,urllib2和requests两种方式

fuxuemingzhu
2017/7/19镜像同步29 回复
更新 写了篇博客,讲了模拟北邮人论坛登录的分析流程,大家可以看下:http://fuxuemingzhu.me/2017/08/12/byrbbs-login/ -------分割线------ 本来这个帖子是问问题的,既然问题解决了,那就把代码分享出来吧~~ =========================下面是urllib2的=================== ```Python import cookielib import urllib2 import urllib auth_url = 'https://bbs.byr.cn/user/ajax_login.json' data = {'id': '*********', 'passwd': '*********', } post_data = urllib.urlencode(data) my_header = {'x-requested-with': 'XMLHttpRequest'} cookieJar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar)) urllib2.install_opener(opener) req = urllib2.Request(auth_url, post_data, headers=my_header) b = opener.open(req) print(str(b.read()).encode('utf-8')) user_url = "https://bbs.byr.cn/user/query/zc199102.json" req_user = urllib2.Request(user_url, headers=my_header) a = opener.open(req_user) print(str(a.read()).encode('utf-8')) ``` ==================代码分割线,下面的是requests的=================== ```Python import requests r_url = 'https://bbs.byr.cn/user/ajax_login.json' my_header = {'x-requested-with': 'XMLHttpRequest'} byr_data = {'id': '********', 'passwd': '********'} s = requests.Session() r = s.post(r_url, data=byr_data, headers=my_header) b = s.get("https://bbs.byr.cn/user/query/zc199102.json", headers=my_header) print(b.text) ```
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
wht机器人#1 · 2017/7/19
python3没有urllib2包吧
NA机器人#2 · 2017/7/19
居然query超神 【 在 fuxuemingzhu (负雪明烛||【意涵团】【若晨团】苦研僧) 的大作中提到: 】 : 还是老生常谈的论坛模拟登陆的话题……感觉下面两段代码本质是没有区别的,可是urllib2代码运行时返回:"ajax_msg":"请求方式错误";requests运行能登录成功。。 : 问下为什么会出现这个情况。。 : ...................
q397273499机器人#3 · 2017/7/19
请用https谢谢 urllib在发生跳转时应该是不会继续转发data的。 Note The request will not work as expected if the data object is unable to deliver its content more than once (e.g. a file or an iterable that can produce the content only once) and the request is retried for HTTP redirects or authentication. The data is sent to the HTTP server right away after the headers. There is no support for a 100-continue expectation in the library. 【 在 fuxuemingzhu (负雪明烛||【意涵团】【若晨团】苦研僧) 的大作中提到: 】 : 还是老生常谈的论坛模拟登陆的话题……感觉下面两段代码本质是没有区别的,可是urllib2代码运行时返回:"ajax_msg":"请求方式错误";requests运行能登录成功。。 : 问下为什么会出现这个情况。。 : ...................
fuxuemingzhu机器人#4 · 2017/7/20
用的2.7 【 在 wht 的大作中提到: 】 : python3没有urllib2包吧
fuxuemingzhu机器人#5 · 2017/7/20
噗哈哈哈,忘记把这行代码删除了 【 在 NA 的大作中提到: 】 : 居然query超神
fuxuemingzhu机器人#6 · 2017/7/20
啊,感谢感谢!果然自己眼瞎……看了很久也没注意到网址有问题 【 在 q397273499 的大作中提到: 】 : 请用https谢谢 urllib在发生跳转时应该是不会继续转发data的。 : : Note : ...................
lucashood机器人#7 · 2017/7/20
zc199102 23333333333
fuxuemingzhu机器人#8 · 2017/7/20
【 在 lucashood 的大作中提到: 】 : zc199102 : 23333333333
caobingyu机器人#9 · 2017/7/20
活捉高神 发自「贵邮」