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. not1,not2,bind1st,bind2nd

    例子需要包含头文件 #include <vector> #include <algorithm> #include <functional> bind1st和bin ...

  2. C++关于堆的函数

    建立堆 make_heap(_First, _Last, _Comp) 默认是建立最大堆的.对int类型,可以在第三个参数传入greater<int>()得到最小堆.   在堆中添加数据 ...

  3. [Leetcode] 20. Valid Parentheses(Stack)

    括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...

  4. 将CRUD封装到一个工具类中

    package org.zln.hibernate.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; ...

  5. request.getParameterMap() 获取表单提交的键值对 并且 也能获取动态表单的key

    Map<String,String[]> map = request.getParameterMap();Set<String> keys = map.keySet(); 获取 ...

  6. 日期时间选择器datetimepicker.js

    在做项目中,往往会遇到需要用户输入2014-07-19 09:55:53这样的格式的数据.就是典型的年月日时分秒这样的格式.这个时候,使用datetimepicker会比较简单. DateTimePi ...

  7. CF762D Maximum Path

    题目戳这里. 首先明确一点,数字最多往左走一次,走两次肯定是不可能的(因为只有\(3\)行). 然后我们用\(f_{i,j}\)表示前\(i\)行,第\(i\)行状态为\(j\)的最优解.(\(j\) ...

  8. 【BZOJ 3232】圈地游戏 二分+SPFA判环/最小割经典模型

    最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那 ...

  9. Expect使用小记

    By francis_hao    May 31,2017   本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善.   Expect是一个可以和交互式程序对话的程序 概述 ...

  10. debounce 与 throttle 区别

    原文地址:http://undefinedblog.com/debounce-and-throttle/ 二.什么是debounce    1. 定义 如果用手指一直按住一个弹簧,它将不会弹起直到你松 ...