學習Python爬蟲(七)--Scrapy模擬登錄的post模擬登陸后羔杨,自己寫了模擬登陸知乎首頁的代碼。
from scrapy.spiders import CrawlSpider
from scrapy.selector import Selector
from scrapy.http import Request,FormRequest
class zhihu_login(CrawlSpider):
name = 'zhihu'
allowed_domains = ['www.zhihu.com']
start_urls = ['https://www.zhihu.com/#signin']
#獲得_xsrf屬性
def parse(self, response):
cel = Selector(response)
_xsrf = cel.xpath('//html/body/input[@name="_xsrf"]/@value').extract()[0]
print(_xsrf)
password = '***' #密碼
captcha_type = 'cn'
phone_num = '***' #電話號碼
Formdata = {'_xsrf':_xsrf,
'password':password,
'captcha_type':captcha_type,
'phone_num':phone_num
}
#post訪問豆瓣登陸url
return FormRequest.from_response(response,formdata=Formdata,callback=self.after_login,dont_filter = True)
#對訪問后的頁面進行解析
def after_login(self,response):
cel = Selector(response)
print(response.body())
測試后發(fā)現無效
Paste_Image.png