scrapy知乎模拟登录失败
时间: 2017-06-26来源:开源中国
前景提要
HDC调试需求开发(15万预算),能者速来!>>>
下面的代码模拟登录知乎,在登录的时候没有成功
check_login提示失败:
check_login
{
"r": 1,
"errcode": 1991829,
"data": {"captcha":"验证码会话无效 :(","name":"ERR_VERIFY_CAPTCHA_SESSION_INVALID"},
"msg": "验证码会话无效 :("
}
有没有兄弟有成功的经验,望指点一二,谢谢
# -*- coding: utf-8 -*-
import scrapy
import json
class ZhihuSpider(scrapy.Spider):
name = "zhihu"
allowed_domains = ["www.zhihu.com"]
headers = {
'Host': 'www.zhihu.com',
'Referer': 'http://www.zhihu.com',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
}
def start_requests(self):
# 返回值必须是一个序列
return [scrapy.Request('http://www.zhihu.com/#signin', callback=self.login)]
def login(self, response):
print('-------') # 便于测试
_xsrf = response.xpath('//input[@name="_xsrf"]/ @value ').extract()[0]
print(response.xpath('//input[@name="_xsrf"]/ @value '))
print(_xsrf)
return [scrapy.FormRequest(
url = 'http://www.zhihu.com/login/email', # 这是post的真实地址
formdata={
'_xsrf': _xsrf,
'email': 'xxxxxx', # email
'password': 'xxxxx', # password
'remember_me': 'true',
},
headers=self.headers,
callback=self.check_login,
)]
def check_login(self, response):
print("check_login")
print(response.text)
print("check_login ---")
if json.loads(response.text)['r'] == 0: ####这里提示登录失败
yield scrapy.Request(
'http://www.zhihu.com',
headers=self.headers,
callback=self.page_content,
dont_filter=True,
)
def page_content(self, response):
with open('first_page.html', 'wb') as f:
f.write(response.body)
print('done')

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行