import request

try:

  import cookielib  #python2版本

except:

  import http.cookiejar as cookielib  #python3版本

import re

session=request.session()

session.cookies=cookielib.LWPCookieJar(filename="cookies.txt")  #将cookies存储到本地文件

#加载cookies文件

try:

  session.cookies.load(ignore_discard=True)

except:

  print("cookies未能加载")

User_Agent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"

header={

  "HOST"  :  "www.zhihu.com",

  "Referer"  :  "https://www.zhihu.com",

  "User_Agent"  :  User_Agen"

}

#获取xsrf

def get_xsrf():

  response=session.post("https://www.zhihu.com",headers=header)  #请求网页需带上头文件

  match_obj=re.match(' .*name="_xsrf"  value="(.*?)" ')  #注意使用单双引号

  if match_obj:

    return (match_obj(1))

  else:

    return " "

def get_index():

  response=session.get("https://www.zhihu.com",headers=header)

  with open("index_page.heml",wb) as f:

    f,write(response.text.encode("utf-8"))

  print ("ok")

#模拟知乎登录

def zhihu_login(account,password):

  if re.match("^1\d{10}",account):  #验证账号是否为手机号

    print ("手机登录")

    post_url="https://www.zhihu.com/login/phone_num"

    post_data={

      "_xsrf"  :  get_xsrf(),

      "phone_num"  :  account,

      "password"  :  password

    }

  else:

    if "@" in account:

      print (“邮箱登录”)

      post_url="https://www.zhihu.com/login/email"

      post_data={

      "_xsrf"  :  get_xsrf(),

      "email"  :  account,

      "password"  :  password

    }

  response_text=session.post(post_url,post_data,headers=header)

  session.cookies.save()

#验证是否登录成功

def is_login():

  inbox_url="https://www.zhihu.com/inbox"

  response=session.get(inbox_url,headers=header,allow_redirects=False)

  if response.status_code !=200:

    return False

  else:

    return True

zhihu.login("18782902568","admin123")

get_index()

最新文章

  1. VS-项目发布失败的解决方案1
  2. Mysql Error:1205错误诊断
  3. 太牛X了!神奇的故事 你猜得到开头,却猜不到结尾
  4. windows和linux共享文件
  5. js团购倒计时
  6. (Qt 翻译) QGLSceneNode
  7. BZOJ 4177: Mike的农场( 最小割 )
  8. [QML] Connections元素介绍
  9. hdu 1233 还是畅通project(kruskal求最小生成树)
  10. python网络编程基础知识整理
  11. 使用XStream是实现XML与Java对象的转换(2)--别名
  12. 关于AMD 、CMD、 commonjs的认识
  13. 每日一练ACM 2019.04.13
  14. laravel中如何在模型中自关联?
  15. javascript的倒计时功能中newData().getTime()在iOS下会报错问题解决
  16. adb command
  17. Linux 僵尸进程
  18. Python 类的多继承
  19. Internet History, Technology and Security (Week5.2)
  20. 【LOJ】#2123. 「HEOI2015」最短不公共子串

热门文章

  1. Leetcode 680.验证回文字符串
  2. Python 异步编程笔记:asyncio
  3. truffle开发一个简单的Dapp
  4. Python如何进行中文注释
  5. linux安装mysql之设置远程访问权限
  6. Javascript Array和String的互转换
  7. Linux挂载Win共享文件夹 一
  8. Linux笔记二
  9. 先查出已知的 然后将未知的当做 having里面的条件
  10. #define、const、typedef的区别