Python,Twisted 并使用 cookielib 添加 cookie 到 getPage

发布于 2024-12-08 09:08:04 字数 1775 浏览 1 评论 0原文

下面是我添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-12-15 09:08:05

看起来您在 cookie 参数中传递了 cookiejar 。我找不到该工作的参考。

我尝试传递一个简单的 dict 并且它有效。这是完整的代码:

from twisted.web import client
import pprint

def processPage (page, url):
    print len(page), 'bytes received'
    print 'cookies: ', pprint.pformat(cj)

def printError(error, url):
    print 'ERROR: ', error.getErrorMessage()

def end(ignore):
    reactor.stop()

iUrl = 'http://twistedmatrix.com'

headers = []

cj = {}
client.getPage(iUrl,headers,method='GET',cookies=cj).addCallback(processPage,iUrl).addErrback(printError,iUrl).addBoth(end)


from twisted.internet import reactor
reactor.run()

这是输出:

10660 bytes received
cookies: 
{'trac_form_token': '940bfe8055b63872303017ba',
 'trac_session': '5a3500ec1bc8efad40974a31'}

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:

from twisted.web import client
import pprint

def processPage (page, url):
    print len(page), 'bytes received'
    print 'cookies: ', pprint.pformat(cj)

def printError(error, url):
    print 'ERROR: ', error.getErrorMessage()

def end(ignore):
    reactor.stop()

iUrl = 'http://twistedmatrix.com'

headers = []

cj = {}
client.getPage(iUrl,headers,method='GET',cookies=cj).addCallback(processPage,iUrl).addErrback(printError,iUrl).addBoth(end)


from twisted.internet import reactor
reactor.run()

And here's output:

10660 bytes received
cookies: 
{'trac_form_token': '940bfe8055b63872303017ba',
 'trac_session': '5a3500ec1bc8efad40974a31'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文