我正在尝试访问蜘蛛内的会话cookie.我首先使用蜘蛛登录社交网络:
def parse(self, response):
return [FormRequest.from_response(response,
formname='login_form',
formdata={'email': '...', 'pass':'...'},
callback=self.after_login)]
Run Code Online (Sandbox Code Playgroud)
在after_login,我想访问会话cookie,以便将它们传递到另一个模块(这里是selenium),以进一步处理具有身份验证会话的页面.
我想要这样的东西:
def after_login(self, response):
# process response
.....
# access the cookies of that session to access another URL in the
# same domain with the autehnticated session.
# Something like:
session_cookies = XXX.get_session_cookies()
data = another_function(url,cookies)
Run Code Online (Sandbox Code Playgroud)
遗憾的是,response.cookies 不会返回会话cookie.
我怎样才能获得会话cookie?我正在查看cookie中间件:scrapy.contrib.downloadermiddleware.cookies和scrapy.http.cookies但似乎没有任何直接的方式来访问会话cookie.
关于我原来的问题,这里还有一些细节:
不幸的是,我使用了你的想法,但我没有看到cookie,虽然我知道它们确实存在,因为scrapy.contrib.downloadermiddleware.cookies中间件确实打印出了cookie!这些正是我想要抓取的cookie.
所以这就是我在做的事情:
after_login(self,response)方法在正确认证后接收响应变量,然后我使用会话数据访问URL:
def after_login(self, response):
# testing to see if I can get the session cookies
cookieJar …Run Code Online (Sandbox Code Playgroud) 我正在使用User该类创建一个django应用程序django.contrib.auth.models.
我已经定义了一个模型,称为与用户具有M2M关系的组.我很难检索给定用户所属的组.
这是组定义:
class group(models.Model):
user = models.ForeignKey(User,related_name = 'owner') # the owner
name = models.CharField(max_length=100) # name of the group
# members of the group
members = models.ManyToManyField(User,related_name = 'member')
def __unicode__(self):
return str(self.name)
Run Code Online (Sandbox Code Playgroud)
我想检索用户所属的组到组的成员字段.
这是失败的命令,试图检索特定用户所属的组 - 我不知道为什么 - 你能告诉我吗?(user是一个User实例)
user_groups = user.group_set.all()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
'User' object has no attribute 'group_set'
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
解
我终于找到了解决方案.我不得不用查询进行查询related_name,所以这里是:
groups_member = user.member.all()
Run Code Online (Sandbox Code Playgroud)