Python,Twisted 并使用 cookielib 添加 cookie 到 getPage
下面是我添加 cookie 以使用 getPage 获取页面的代码。这也是我收到的错误消息。如何添加 cookie?如果 cookie 为 None 那么代码就可以工作。
cj = cookielib.CookieJar()
client.getPage(iUrl,headers,method='GET',cookies=cj).addCallback(self.processPage,iUrl).addErrback(self.printError,iUrl)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 59, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 37, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 664, in doConnect
self._connectDone()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 671, in _connectDone
self.protocol.makeConnection(self)
File "/usr/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 459, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/dist-packages/twisted/web/client.py", line 61, in connectionMade
for cookie, cookval in self.factory.cookies.items():
exceptions.AttributeError: CookieJar instance has no attribute 'items'
*--- Failure #133 ---
Failure: exceptions.AttributeError: CookieJar instance has no attribute 'items'
*--- End of Failure #133 -
Below is my code for adding a cookie to get a page using getPage. Also is the error message I get. How to I add a cookie? Is cookie is None then the code works.
cj = cookielib.CookieJar()
client.getPage(iUrl,headers,method='GET',cookies=cj).addCallback(self.processPage,iUrl).addErrback(self.printError,iUrl)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 59, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 37, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 664, in doConnect
self._connectDone()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 671, in _connectDone
self.protocol.makeConnection(self)
File "/usr/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 459, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/dist-packages/twisted/web/client.py", line 61, in connectionMade
for cookie, cookval in self.factory.cookies.items():
exceptions.AttributeError: CookieJar instance has no attribute 'items'
*--- Failure #133 ---
Failure: exceptions.AttributeError: CookieJar instance has no attribute 'items'
*--- End of Failure #133 -
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您在 cookie 参数中传递了
cookiejar
。我找不到该工作的参考。我尝试传递一个简单的
dict
并且它有效。这是完整的代码:这是输出:
Looks like you're passing a
cookiejar
in the cookie argument. I can't find a reference for that working.I've tried passing a simple
dict
and it works. Here's the full code:And here's output: