English 中文(简体)
如何从报废反应中获取 co,并安排 co子满足下一次要求?
原标题:How to get cookie from scrapy response and set the cookie to the next request?

I have disabled the Default Scrapy cookie option, so that i have to set it manually.

COOKIES_ENABLED = False
COOKIES_DEBUG = True

现在,我需要把作为同一地点答复而得到的价值 set。 我能够像以下那样获得厨师。

cookie = response.headers.getlist( Set-Cookie )[0].split(";")[0].split("=")

now i am trying to set it to the form request by

FormRequest.from_response(response,
                formdata={"username": "asldkfs", "pass": "slskd"},
                cookies={cookie[0]:cookie[1]},
                meta = { dont_redirect : True, handle_httpstatus_list : [302]},
                callback=self.redirection)

def redirection(self,response): 
    self.log("redirection")
    self.log(response.headers)               
    self.log("Cookie2")
    cook1 = response.headers.getlist( Set-Cookie )[0].split(";")[0].split("=")
    self.log(cook1)        
    self.log("end cookie2")
    return Request("http://something.net/some/sa/"+response.headers.getlist( Location )[0],cookies={cook1[0]:cook1[1]},
        callback=self.check_login_response)

.
.
.

因此,我不能 set。 是否还需要确定任何其他价值或问题是什么?

问题回答

The cookies argument only works if you have COOKIES_ENABLED set to True, since CookiesMiddleware handles it.

因此,你必须把手册放在负责人身上:

cookie = response.headers.getlist( Set-Cookie )[0].split( ; )[0]

FormRequest.from_response(response,
            formdata={"username": "asldkfs", "pass": "slskd"},
            headers={ Cookie : cookie}, # <---
            meta = { dont_redirect : True, handle_httpstatus_list : [302]},
            callback=self.redirection)

我认为,如果你残疾,你就无法与厨师合作。

If you want a different way other than response.headers.getlist( Set-Cookie ), you can do something like:

from scrapy.http.cookies import CookieJar


def response_cookies(response):
    """
    Get cookies from response
    
    @param response scrapy response object
    @return: dict
    """

    obj = CookieJar(policy=None)
    jar = obj.make_cookies(response, response.request)
    cookies = {}
    for cookie in jar:
        cookies[cookie.name] = cookie.value
    return cookies

如果有的话,在<代码>jar = 斜线_cookies(response, **response.request**)中,系统返回平衡没有从名单上获得功能。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签