用FromRequest模拟登陆知乎网站
实例
  默认登陆成功以后的请求都会带上cookie
# -*- coding: utf-8 -*-
import re
import json
import datetime try:
import urlparse as parse
except:
from urllib import parse import scrapy class ZhihuSpider(scrapy.Spider): name = "zhihu"
allowed_domains = ["www.zhihu.com"]
start_urls = ['https://www.zhihu.com/']
headers = {
"HOST": "www.zhihu.com",
"Referer": "https://www.zhizhu.com",
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"
} def parse(self, response):
pass def start_requests(self):
return [scrapy.Request('https://www.zhihu.com/#signin', headers=self.headers, callback=self.login)] 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": "",
"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_filter=True, headers=self.headers)

最新文章

  1. 深入Java虚拟机--判断对象存活状态
  2. 在Mac OS X上安装ASP.NET 5(译文)
  3. [LeetCode] Minimum Path Sum 最小路径和
  4. GoodReader跨域访问HT for Web手册
  5. 15款最佳的MySQL管理工具和应用程序
  6. spring 源码下载地址
  7. 微软分布式框架Orleans开源了
  8. Apache与Nginx服务器对比
  9. 第一百九十六天 how can I 坚持
  10. 关于I/O的那点事
  11. Objective-C基础知识点总结
  12. IE6下的怪异解析知识点补充
  13. log(n)在第一时间,以确定该阵列i小号码
  14. MySQL备份说明
  15. nginx HTTP/2.0 配置
  16. 学习sharding-jdbc 分库分表扩展框架
  17. 机器翻译评价指标 — BLEU算法
  18. django restframework Serializer field
  19. 2018.7.3 lnmp一键安装包无人值守版本 php7.2 + nginx1.14.0 + mariadb5.5 + centos7.1(1503) 环境搭建 + Thinkphp5.1.7 配置
  20. 2018-2019-1 20189221《Linux内核原理与分析》第一周作业

热门文章

  1. ndarray数组自动创建
  2. CAD交互绘制矩形批注(网页版)
  3. shell补充知识点
  4. ios 团购信息客户端demo(二)
  5. noip_最后一遍_3-数据结构
  6. linux配置MySql表名不区分大小写
  7. linux内核启动修复
  8. Springboot(一)-IDEA搭建springboot项目(demo)
  9. PAT Basic 1059
  10. HDU 3045 DP 斜率优化 Picnic Cows