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()

request模拟知乎登录(无验证码机制)的更多相关文章

  1. scrapy模拟知乎登录(无验证码机制)

    ---恢复内容开始--- spiders 文件夹下新建zhihu.py文件(从dos窗口中进入虚拟环境,再进入工程目录之后输入命令 scrapy genspider zhihu www.zhihu.c ...

  2. (转)request模拟知乎登录(无验证码机制

    原文:http://www.itnose.net/detail/6755805.html import request try: import cookielib #python2版本 except: ...

  3. htmlunit 模拟登录 无验证码

    1.模拟登录csdn,最开始的时候使用的是httpclient,网上的所有模拟登录csdn的版本都是找到lt/execution/event_id.连同用户名及密码 一起发送即可,但是目前的csdn的 ...

  4. 使用selenium模拟知网登录

    之前都是用phantomjs和selenium模拟浏览器动作的,后来phantomjs不再更新,就转用chrome了 本次模拟登录的网站是中国知网http://login.cnki.net/login ...

  5. Python模拟知乎登录

    # -*- coding:utf-8 -*- import urllib import urllib2 import cookielib import time from PIL import Ima ...

  6. 8-python模拟登入(无验证码)

    方式: 1.手动登入,获取cookie 2.使用cookielib库 和 HTTPCookieProcessor处理器 #_*_ coding: utf-8 _*_ ''' Created on 20 ...

  7. 新版知乎登录之post请求

    前言 在上一篇文章中给大家讲解了requests发送post请求的几种方式,并分析了一些使用陷阱. 疑惑 在文章发表之后,有朋友给我留言说,知乎登录就没有使用提交Form表单(application/ ...

  8. python爬虫scrapy框架——人工识别知乎登录知乎倒立文字验证码和数字英文验证码

    目前知乎使用了点击图中倒立文字的验证码: 用户需要点击图中倒立的文字才能登录. 这个给爬虫带来了一定难度,但并非无法解决,经过一天的耐心查询,终于可以人工识别验证码并达到登录成功状态,下文将和大家一一 ...

  9. 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别

    第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别 第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://gith ...

随机推荐

  1. Python登录小程序

    ------------------------------------------------- 主要实现功能 1.用户输入用户名,在用户名文件中查找对应的用户,若无对应用户名则打印输入错误 2.用 ...

  2. skip-grant-tables 修改linux的mysql忘记root密码

    skip-grant-tables 修改linux的mysql忘记root密码 今天修改mysql中的admin用户权限,在执行update user set host =' %' where use ...

  3. HDU 4588 Count The Carries(数学统计)

    Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...

  4. maven环境变的配置(复制自己看)

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具.由于 Maven 的缺省构建 ...

  5. lintcode-98-链表排序

    98-链表排序 在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序. 样例 给出 1->3->2->null,给它排序变成 1->2->3->nu ...

  6. union的代码有点难理解额

    union跟位域都可以节省内存,而且union在某些地方还能起到更好看的效果? 比如: struct Matrix { union { struct { float _f11, _f12, _f13, ...

  7. CFS 调度器

    CFS调度器的原理明白了但是有个地方,搜遍了整个网络也没找到一个合理的解释: if (delta > ideal_runtime) resched_task(rq_of(cfs_rq)-> ...

  8. 多线程 定时器 Timer TimerTask

    定时器是一种特殊的多线程,使用Timer来安排一次或者重复执行某个任务 package org.zln.thread; import java.util.Date; import java.util. ...

  9. 大并发量订单处理的 KafKa部署

    大并发量订单处理的 KafKa部署总结 今天要介绍的是消息中间件KafKa,应该说是一个很牛的中间件吧,背靠Apache 与很多有名的中间件搭配起来用效果更好哦 ,为什么不用RabbitMQ,因为公司 ...

  10. 魔法物品(magic.pas/c/cpp)

    有两种类型的物品:普通物品和魔法物品.普通物品没有魔法属性,而魔法物品拥有一些魔法属性.每种普通物品有一个价值P,但每种魔法物品有两种价值:鉴定前的价值P.和鉴定后的价值P2(当然,P2总是大于P.) ...