Python使用Requests第三方库自动登陆知乎
2016-01-23 20:32
585 查看
写爬虫就是我学习python的动力,刚开始学习的时候是用python自带的urllib和urllib2的库写爬虫,感觉有点繁琐,今天学习了Requests库感觉用起来比之前用的库方便多了。对网页的分析用的是BeautifulSoup4,之前用正则表达式写麻烦还伤脑经,有便利的工具于是就用了。
准备工具:python2.7
Request
Beautiful
第一步:分析登录请求
首先将密码输错,发现浏览器发送了一个“email”的post请求
header头的内容就不用介绍了,直接看post表单中的内容,email对应登录的账号(这是使用邮箱登陆的,如果使用手机登录的话是不一样的,是phone_num);password对应的就是密码了;captcha对应的就是验证码,图中的captcha.gif?r=.....请求就是请求验证码的;remember_me对应的是是否记住登录密码,这几个内容好办,还有最后一个_xsrf对应了一串奇怪的东西,通过查看源代码发现,这一串奇怪的东西在源码中对应着一个input标签
这个应该是知乎的一种防爬虫机制吧。
所以要做的工作已经很明显了,首先就是要去获取到这个xsrf的内容,然后获取一下验证码,最后将这几个内容包装一下,发送给服务器就可以登陆成功了。
第二部:编写程序
我使用Requests的session对象,那样就不用去操心cookie的事情了
1.获取xrsf内容
2.获取验证码,将验证码的图片保存下来,自己输入验证码
3.设置请求header
4.封装数据
5.发送请求
如果成功或返回一个json格式的数据
完整代码:
准备工具:python2.7
Request
Beautiful
第一步:分析登录请求
首先将密码输错,发现浏览器发送了一个“email”的post请求
header头的内容就不用介绍了,直接看post表单中的内容,email对应登录的账号(这是使用邮箱登陆的,如果使用手机登录的话是不一样的,是phone_num);password对应的就是密码了;captcha对应的就是验证码,图中的captcha.gif?r=.....请求就是请求验证码的;remember_me对应的是是否记住登录密码,这几个内容好办,还有最后一个_xsrf对应了一串奇怪的东西,通过查看源代码发现,这一串奇怪的东西在源码中对应着一个input标签
这个应该是知乎的一种防爬虫机制吧。
所以要做的工作已经很明显了,首先就是要去获取到这个xsrf的内容,然后获取一下验证码,最后将这几个内容包装一下,发送给服务器就可以登陆成功了。
第二部:编写程序
我使用Requests的session对象,那样就不用去操心cookie的事情了
1.获取xrsf内容
url = "http://www.zhihu.com" s = requests.session() html = s.get(url) soup = BeautifulSoup(html.text,'html.parser') xrsf = soup.find('input') print(xrsf.get('value'))
2.获取验证码,将验证码的图片保存下来,自己输入验证码
#验证码 yanzhen_url = 'https://www.zhihu.com/captcha.gif?r='+str(int(time.time()*1000)) haha = s.get(yanzhen_url,headers = head) with open('code.jpg','wb') as f: f.write(haha.content) print '请输入验证码' yanzhen = raw_input() print '验证码:'+yanzhen
3.设置请求header
<span style="white-space:pre"> </span>head ={'Accept':'*/*', 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With':'XMLHttpRequest', 'Referer':'http://www.zhihu.com', 'Accept-Language':'zh-CN', 'Accept-Encoding':'gzip, deflate', 'User-Agent':'Mozilla/5.0(Windows NT 6.1;WOW64;Trident/7.0;rv:11.0)like Gecko', 'Host':'www.zhihu.com'}
4.封装数据
login_data = {'_xsrf':xrsf, 'password':password, 'captcha':yanzhen, 'email':email, 'remember_me':'true'}
5.发送请求
r = s.post('https://www.zhihu.com/login/email',data=login_data,headers = head) print r.text
如果成功或返回一个json格式的数据
完整代码:
#-*-coding:utf-8-*-
import requests
import urllib
import time
import urllib2
import sys
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
email = 'xxxxxxxx@qq.com'
password = 'xxxxxxx'
head ={'Accept':'*/*',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With':'XMLHttpRequest',
'Referer':'http://www.zhihu.com',
'Accept-Language':'zh-CN',
'Accept-Encoding':'gzip, deflate',
'User-Agent':'Mozilla/5.0(Windows NT 6.1;WOW64;Trident/7.0;rv:11.0)like Gecko',
'Host':'www.zhihu.com'}
#获取xrsf
url = "http://www.zhihu.com"
s = requests.session()
html = s.get(url)
soup = BeautifulSoup(html.text,'html.parser')
xrsf = soup.find('input')
print(xrsf.get('value'))
#验证码 yanzhen_url = 'https://www.zhihu.com/captcha.gif?r='+str(int(time.time()*1000)) haha = s.get(yanzhen_url,headers = head) with open('code.jpg','wb') as f: f.write(haha.content) print '请输入验证码' yanzhen = raw_input() print '验证码:'+yanzhen
login_data = {'_xsrf':xrsf, 'password':password, 'captcha':yanzhen, 'email':email, 'remember_me':'true'}
r = s.post('https://www.zhihu.com/login/email',data=login_data,headers = head) print r.text
#验证一下是否登录成功
r = s.get('http://www.zhihu.com/')
print r.text
相关文章推荐
- eclipse环境搭建之三:Python
- Python collections.defaultdict() 与 dict的使用和区别
- 【python】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- Python成长之路第一篇(2)-初识列表和元组
- python爬虫之赞同知乎用户所有答案以及给用户发私信
- Python成长之路第一篇(2)-初识列表和元组
- [Leetcode]91. Decode Ways@python
- Python 插件安装
- 简单理解python下的字符串
- python fabric模块
- 网站渗透常用到的Python小脚本
- 网站渗透常用到的Python小脚本
- Python pyenv
- Python -- 文件处理
- Python环境的搭建
- python-opencv的一段比较稳定的摄像头视频读取代码
- python中numpy对函数进行矢量化转换
- Python操作MySQL案例
- python多进程
- leetcode之Merge k Sorted Lists