文章标题
来源:互联网 发布:怎样挑选淘宝剪标衣服 编辑:程序博客网 时间:2024/06/04 01:31
import scrapyimport timeclass ZhihuSpider(scrapy.Spider): name = 'zhihu' allowed_domains = ['zhihu.com'] start_urls = ['https://www.zhihu.com/#signin'] def parse(self, response): # 1.先发get请求,获取登录参数 _xsrf = response.css("input[name=_xsrf]::attr(value)").extract()[0] #response.xpath("//input[@name='_xsrf']/@value").extract()[0] # 2. 处理验证码请求 captcha_url = "https://www.zhihu.com/captcha.gif?r=" + str(int(time.time() * 1000)) + "&type=login" yield scrapy.Request(captcha_url, meta = {"_xsrf" : _xsrf}, callback = self.zhihu_login) def parse_captcha(self, response): with open("captcha.png", "wb") as f: f.write(response.body) captcha = raw_input("请输入验证码:") # 返回验证码字符串 return captcha # 这个response是验证码图片的响应 def zhihu_login(self, response): _xsrf = response.meta["_xsrf"] data = { "_xsrf":_xsrf, "email":"123636274@qq.com", "password":"ALARMCHIME", "remember_me": "True", "captcha": self.parse_captcha(response) } # 3. 获取所有必须的数据后,发送post请求登录 yield scrapy.FormRequest( "https://www.zhihu.com/login/email", formdata = data, callback = self.after_login ) def after_login(self, response): #print response.body # 4. 登录成功后,已经获取了Cookie,再去发送其他页面的请求即可 yield scrapy.Request("https://www.zhihu.com/settings/account", callback = self.parse_item) def parse_item(self, response): with open("my_zhihu.html", "w") as f: f.write(response.body)
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- HDU2051Bitset
- 线程安全,关键字synchronized的其他基本特性(可重入锁)
- 2017.11-上海威盛电子有限公司面试
- 迭代函数系统生成分形
- 数据结构 之 8中常用排序
- 文章标题
- BZOJ 2946: [Poi2000]公共串 后缀数组
- 【趣味连载】攻城狮上传视频与普通人上传视频:序
- ArrayList的contains方法
- 天耀18期
- Mohican_12/5 C语言—浮点 运算符
- 结合redis设计与实现的redis源码学习-22-集群(cluster.c)
- 写给未来的自己
- Jmockit使用详解之Faking