Access session cookie in scrapy spiders

前端 未结 3 889
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 17:07

I am trying to access the session cookie within a spider. I first login to a social network using in a spider:

    def parse(self, response):

        retur         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-28 17:52

    A classic example is having a login server, which provides a new session id after a successful login. This new session id should be used with another request.

    Here is the code picked up from source which seems to work for me.

    print 'cookie from login', response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")[1]
    

    Code:

    def check_logged(self, response):
    tmpCookie = response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")[1]
    print 'cookie from login', response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")[1]
    cookieHolder=dict(SESSION_ID=tmpCookie)
    
    #print response.body
    if "my name" in response.body:
        yield Request(url="<>",   
            cookies=cookieHolder,
            callback=self."<>")
    else:
        print "login failed"
            return 
    

提交回复
热议问题