码迷,mamicode.com
首页 > 其他好文 > 详细

scrapy模拟登陆小例子

时间:2018-01-25 00:09:46      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:safari   mda   chrome   bbr   blog   页面   self   logs   ade   

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request,FormRequest


class IqianSpider(scrapy.Spider):
    name = ‘iqian‘
    allowed_domains = [‘edu.iqianxxx.com‘]
    # start_urls = [‘http://edu.iqianxxx.com/‘]
    headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER‘}
    
    # 入口
    def start_requests(self):
        start_url = "http://edu.iqianxxx.com/index_user_login.html"
        return [Request(url=start_url, meta={"cookiejar": 1}, callback=self.login)]

    def login(self, response):
        data = {
            "number": "xxxx",
            "passwd": "xxxx",
        }
        print("登录中...............")
        return [FormRequest.from_response(response,
                                        meta={"cookiejar": response.meta["cookiejar"]},
                                        headers=self.headers,
                                        formdata=data,
                                        callback=self.next1,
        )]
    
    #需要在跳转一次,以防中间页面
    def next1(self, response):
        yield Request(url="http://edu.iqianxxx.com/index_user_index.html", callback=self.next2)

    def next2(self, response):
        print(response.url)
        pass

scrapy模拟登陆小例子

标签:safari   mda   chrome   bbr   blog   页面   self   logs   ade   

原文地址:https://www.cnblogs.com/lilied/p/8343773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!