Python网络爬虫之模拟登录(以知乎为例)

参考:Web Crawler with Python - 08.模拟登录 (知乎)

三个问题:

    在实践时,发现该行报错:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. _xsrf = BeautifulSoup(session.get('https://www.zhihu.com/#signin').content).find('input', attrs={'name''_xsrf'})['value']  
    于是在chrome下F12再次分析一下登录过程之后,在requests的headers中加入User-Agent,发现可以获得_xsrf 字段。

    接下来获取验证码和请求时同理加上User-Agent。




    之后再获取验证码时,发现获得的结果如下:

    ERR_VERIFY_CAPTCHA_SESSION_INVALID


    再次分析获得验证码的请求(更新验证码):

    考虑请求时使用的requests的session机制,已经携带了cookie信息。于是怀疑是url的问题。

    改成如下解决:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. captcha_content = session.get('http://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000), headers=headers).content  



    最后修改断言,返回结果如下:



代码:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/python  
  2. # -*- coding: utf-8 -*-  
  3.   
  4. import time  
  5. import requests  
  6. from bs4 import BeautifulSoup  
  7.   
  8.   
  9. headers = {  
  10. 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',  
  11. # 'Referer':'https://www.zhihu.com/',  
  12. # 'X-Requested-With': 'XMLHttpRequest',  
  13. # 'Origin':'https://www.zhihu.com'  
  14. }  
  15.   
  16. def login(username, password, kill_captcha):  
  17.     session = requests.session()  
  18.     _xsrf = BeautifulSoup(session.get('https://www.zhihu.com/#signin', headers=headers).content).find('input', attrs={'name''_xsrf'})['value']  
  19.     session.headers.update({'_xsrf':str(_xsrf)})  
  20.     #加入type=login 否则:ERR_VERIFY_CAPTCHA_SESSION_INVALID  
  21.     captcha_content = session.get('http://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000), headers=headers).content  
  22.     data = {  
  23.         '_xsrf': _xsrf,  
  24.         'password': password,  
  25.         'captcha': kill_captcha(captcha_content),  
  26.         'email': username,  
  27.         'remember_me''true'  
  28.         # 字典的键值对顺序可以随机  
  29.     }  
  30.     print data  
  31.     resp = session.post('http://www.zhihu.com/login/email', data=data, headers=headers).content  
  32.     # 登录成功  
  33.     print 'resp\n',resp  
  34.     assert r'\u767b\u5f55\u6210\u529f' in resp  
  35.     return session  
  36.   
  37.   
  38. def kill_captcha(data):  
  39.     with open('1.gif''wb') as fp:  
  40.         fp.write(data)  
  41.     return raw_input('captcha : ')  
  42.   
  43. if __name__ == '__main__':  
  44.     session = login('email''password', kill_captcha)  
  45.     print BeautifulSoup(session.get("https://www.zhihu.com",headers=headers).content).find('span'class_='name').getText()  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值