---恢复内容开始---

spiders 文件夹下新建zhihu.py文件(从dos窗口中进入虚拟环境,再进入工程目录之后输入命令 scrapy genspider zhihu www.zhihu.com)

#zhihu.py

import scrapy

import re

import json

from Item import ZhihuQuestionItem,ZhihuAnswerItem

import datatime

from scrapy.loader import ItemLoader

try:

  import urlparse as parse

except:

  from urllib import parse

class ZhuhuSpider(scrapy.Spider):

  name='zhihu'

  allow_domains=["www.zhihu.com"]

  start_urls=["http://www.zhihu.com/"] 

  headers={

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

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

  "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"

  }

  start_answer_url="https://www.zhihu.com/api/v4/questions/{0}/answers?      sort_by=default&include=data%5B%2A%5D.is_normal%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccollapsed_counts%2Creviewing_comments_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Cmark_infos%2Ccreated_time%2Cupdated_time%2Crelationship.is_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cupvoted_followees%3Bdata%5B%2A%5D.author.is_blocking%2Cis_blocked%2Cis_followed%2Cvoteup_count%2Cmessage_thread_token%2Cbadge%5B%3F%28type%3Dbest_answerer%29%5D.topics&limit={1}&offset={2}"  #question 第一页answer请求的url

def parse(self,response):

  """

  提取出html页面中的所有url,并跟踪这些url进行进一步爬取

  如果提取出的url中格式为/requestion/xxx,就下载之后直接进入解析函数

  """

  all_urls=response.css("a::attr(href)").extract()

  all_urls=[parse.urljoin(response.url,url) for url in all_urls]

  all_urls=fliter(lambda x:True if x.startswith("https") else False,all_urls)

  for url in all_urls:

    match_obj=re.match("(.*zhihu.com/requestion/(\d))(/|$).*",url)

    if match_obj:

  #如果提取到requestion相关页面,交由parse_question进行解析

      request_url=match_obj.group(1)

      yield scrapy.Request(request_url,headers=self.headers,callback=self.parse_question)

    else:

  #如果未提取到相关页面,则直接进一步跟踪

      yield scrapy.Request(url,headers=self.headers,callback=self.parse)

def parse_question(self,response):

  #处理question页面,从页面中提取出具体的question item

  match_obj=re.match("(.*zhuhu.com/question/(\d))(/|$).*",response.url)

  if match_obj:

     question_id=int(match_obj.group(2))

  item_loader=ItemLoader(item=zhuhuQuestionItem(),response=response)

  if "QuestionHeader-title" in response.text:  #处理新版本  

     item_loader.add_css("title","h1.QuestionHeader-title::text")

     item_loader.add_css("content",".QuestionHeader-detail")

     item_loader.add_value("url",response.url)

     item_loader.add_value("zhuhu_id",question_id)

     item_loader.add_css("answer_num",".List-headerText span::text")

     item_loader.add_css("comment_num",".QuestionHeader-actions button::text")

     item_loader.add_css("watch_user_num",".NumberBoard-value::text")   

     item_loader.add_css("topic",".QuestionHeader-topics .Popover div::text")     

  else:  #处理旧版本页面item的提取

    item_loader.add_xpath("title","//*[@id='zh-question-title']/h2/a/text()|//*[@id='zh-question-title']/h2/span/text()")

    item_loader.add_css("content","#zh-question-detail")

    item_loader.add_value("url",response.url)

    item_loader.add_value("zhuhu_id",question_id)

    item_loader.add_css("answer_num", "#zh-question-answer-num::text")

    item_loader.add_css("comment_num","#zh-question-meta-wrap a[name='addcomment']::text")

    item_loader.add_xpath("watch_user_num","//*[@id='zh-question-side-header-wrap']/text()|//*[@class='zh-question-followers-sidebar']/div/a/strong/text()")

    item_loader.add_css("topic",".zm-tag-editor-labels a::text") 

  question_item=item_loader.load_item()

  yield scrapy.Request(self.start_answer_url,format(question_id,20,0),headers=self.headers,callback=self.parse_answer)

  yield question_item

def  parse_answer(self,response):

   #处理question中的answer

  ans_json=json.load(response.text)

  is_end=ans_json["paging"]["is_end"]

  next_url=ans_json["paging"]["next"]

  #提取answer的具体字段

  for answer in ans_json["data"]:

    answer_item=ZhihuAnswerItem()

    answer_item["zhihu_id"]=answer["id"]

    answer_item["url"]=answer["url"]

    answer_item["question_id"]=answer["question"]["id"]

    answer_item["author_id"] = answer["author"]["id"] if "id" in answer["author"] else None

    answer_item["content"] = answer["content"] if "content" in answer else None

    answer_item["praise_num"]=answer["voteup_count"]

    answer_item["comment_num"]=answer["comment_count"]

    answer_item["creat_time"]=answer["created_time"]

    answer_item["update_time"]=answer["update_time"]

    answer_item["crawl_time"]=datatime.datatime.now()   

    yield answer_item

    if not is_end:

      yield scrapy.Request(next_url,headers=self.headers,callback=self.answer.parse_answer) 

#重写start_Request方法

def start_requests(self):

  return [scrapy.Request("https://www.zhihu.com/#signin",headers=self.headers,callback=self.login)]  #使用scrapy.Request一定要使用回调函数,否则会默认回调parse(self,response)

def login(self,response):

  response_text=response.text

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

  xsrf=""

  if match_obj:

    xsrf=(match_obj.group(1))

  if xsrf:

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

    post_data={

      "_xsrf"  :  xsrf,

      "phone_num"  :  "18282902586",

      "password"  :  "admin123"

    }

    return [scrapy.FormRequest(

      url=post_url,

      formdata=post_data,

      headers=self.headers,

      callback=self.check_login

      )]

def check_login(self,response):

  text_json=json.loads(response.text)

  if "msg" in text_json and text_json["msg"]=="登陆成功":

    for url in self.start_urls:

      yield scrapy.Request(url,dont.fliter=True,headers=self.headers)

---恢复内容结束---

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

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

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

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

    import request try: import cookielib #python2版本 except: import http.cookiejar as cookielib #python3版 ...

  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. 利用scrapy模拟登录知乎

    闲来无事,写一个模拟登录知乎的小demo. 分析网页发现:登录需要的手机号,密码,_xsrf参数,验证码 实现思路: 1.获取验证码 2.获取_xsrf 参数 3.携带参数,请求登录 验证码url : ...

随机推荐

  1. ubuntu中 VI 方向键、删除键问题

    这两天重新装的ubuntu系统,发觉使用VI时,方向键按下去后变成ABCD,删除键无效.网上搜寻一番,应该是VI软件本身的问题,顾卸载重装即可,步骤如下: 1.执行命令 sudo apt-get re ...

  2. matlab mex 小o -o 出错

    https://github.com/kyamagu/mexopencv/issues/117 就是说2014a以后的版本mex   -o 选项变成了 -output 蛋疼,这有什么好改的.找了好久才 ...

  3. 在easyUI开发中,出现jquery.easyui.min.js函数库问题

    easyUI是jquery的一个插件,是民间的插件.easyUI使用起来很方便,里面有网页制作的最重要的三大方块:javascript代码.html代码和Css样式.我们在导入easyUI库后,可以直 ...

  4. HashMap和Hashtable的区别(转载)

    转载声明:转载自原文http://www.importnew.com/7010.html HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是 ...

  5. Java基础知识-去重

    java基础知识-去掉list集合中的重复元素: 思路: 首先新建一个容器resultList用来存放去重之后的元素 然后遍历sourceList集合中的元素 判断所遍历的元素是否已经存在于resul ...

  6. Hadoop Yarn on Docker

    搭建Hadoop Yarn on Docker 一.概览 Docker基于Linux Container技术整合了一堆易用的接口用于构建非常轻量级的虚拟机.Docker Container Execu ...

  7. SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase

    sqlserver 插入数据的时候 插入失败,报错内容为 “SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, ...

  8. VisualStudio2010项目转换为VisualStudio2005项目:解决方案和工程项目文件转换方法(2)

    因为我现在不喜欢把一篇博客写的很长很长,这篇博客是接着上一篇博客来写的.上一篇文章我很详细的说明了修改项目文件解决方案的过程.这篇文章我就说说项目中的项目文件该怎么修改.因为我平日里主要做的是ASP. ...

  9. LeetCode -- Tiangle

    Question: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...

  10. 2017南开ACM校赛(网络赛) 民间题解

    orz 首先说一下这个只是民间题解,可能会有很多错误 程序还没有评测,所以可能存在问题 C题比赛的时候没想到..后来发现是个模板题,所以没有代码 希望这份题解能对读者有所启发吧... A题 直接倒序枚 ...