scrapy的post登录:renren
# -*- coding: utf-8 -*- import scrapy class RenrenSpider(scrapy.Spider): name = 'renren' allowed_domains = ['renren.com'] # 修改起始的请求 start_urls = ['http://www.renren.com/PLogin.do'] def start_requests(self): # 构建url url = self.start_urls[0] # 构建请求数据 post_data = { 'email': '17173805860', 'password': '1qaz@WSX3edc' } # 发送请求 yield scrapy.FormRequest(url,formdata=post_data) def parse(self, response): with open('renren.html','wb')as f: f.write(response.body)
scrapy 如何发送post请求。代码如上。
方法二:针对从首页重定向到登录页面的post登录
# -*- coding: utf-8 -*- import scrapy class RenrenFormSpider(scrapy.Spider): name = 'renren_form' allowed_domains = ['renren.com'] start_urls = ['http://renren.com/'] def parse(self, response): # 构建post数据 post_data = { 'email': '17173805860', 'password': '1qaz@WSX3edc' } yield scrapy.FormRequest.from_response(response,formdata=post_data,callback=self.after_login) def after_login(self, response): with open('renrne2.html','w')as f: f.write(response.text)