python3使用requests登录人人影视网站的方法

早就听说requests的库的强大,只是还没有接触,今天接触了一下,发现以前使用urllib,urllib2等方法真是太搓了……

这里写些简单的使用初步作为一个记录

本文继续练习使用requests登录网站,人人影视有一项功能是签到功能,需要每天登录签到才能升级。

下面的代码python代码实现了使用requests登录网站的过程。

以下是使用fiddler抓包得到完整的http请求头:

post http://www.zimuzu.tv/user/login/ajaxlogin http/1.1
host: www.zimuzu.tv
connection: keep-alive
content-length: 102
accept: application/json, text/javascript, */*; q=0.01
origin: http://www.zimuzu.tv
x-requested-with: xmlhttprequest
user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/50.0.2661.94 safari/537.36
content-type: application/x-www-form-urlencoded
dnt: 1
referer: http://www.zimuzu.tv/user/login
accept-encoding: gzip, deflate
accept-language: zh-cn,zh;q=0.8,en;q=0.6
cookie: phpsessid=st40f3vohv6q16ec3atekimba0; last_item:10733=game.of.thrones.s06e01.the.red.woman.1080p.web-dl.dd5.1.h.264-ntb.mkv; last_item_date:10733=1461856566; mykeywords=a%3a2%3a%7bi%3a0%3bs%3a6%3a%22%e7%a1%85%e8%b0%b7%22%3bi%3a1%3bs%3a14%3a%22silicon+valley%22%3b%7d; zmz_rich=2
account=你的用户名&password=你的密码&remember=1&url_back=http%3a%2f%2fwww.zimuzu.tv%2fuser%2fsign

python3使用requests登录人人影视网站.py代码:

“””
python3使用requests登录人人影视网站.py
2016年5月11日 07:33:59 codegay
参考资料requests文档:
http://cn.python-requests.org/zh_cn/latest/
四种常见的 post 提交数据方式
https://imququ.com/post/four-ways-to-post-data-in-http.html
“””
import re
import requests
#requests 安装命令:pip install requests
loginurl=’http://www.zimuzu.tv/user/login/ajaxlogin’
surl=’http://www.zimuzu.tv/user/sign’
httphead={
‘accept’:’application/json, text/javascript, */*; q=0.01′,
‘origin’:’http://www.zimuzu.tv’,
‘x-requested-with’:’xmlhttprequest’,
‘user-agent’:’mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/50.0.2661.94 safari/537.36′,
‘content-type’: ‘application/x-www-form-urlencoded’,
}
data=”account=用户名&password=密码&remember=1″
session=requests.session()
login=session.post(loginurl,data=data,headers=httphead)
print(login.cookies)#打印登录后取得到cookies对象
print(login.json())
getstat=session.get(surl).text.split(“\n”) #访问签到页面,显示最近三次登录时间
[print(r) for r in getstat if “三次登录时间” in r]

对比其中两者可见,有一些http头省略掉也能达到目的,毕竟每次手动请求头感觉挺麻烦的。

在fidder 中connection: keep-alive content-length: 两项不能省略,ncat之类的工具中也不能省略content-length,如果改动了post的数据,需要手动修正content-length的值。

在python中可以省略掉content-length,我猜python已经帮我们处理了。

关于python3使用requests登录人人影视网站的方法就给大家介绍这么多,希望对大家有所帮助!

Posted in 未分类

发表评论