cookielib

Discussion in 'Python' started by sri2097, Feb 22, 2006.

  1. sri2097

    sri2097 Guest

    Hi, I need to get to a particular page in a website. The site uses
    cookeis and naturally I had to use cookielib since urllib2 does not
    support cookies. But even after adding the cookies to the headers, I
    keep getting a error message from the web-site saying that - 'My
    Browser has disabled cookies and I cannot access the page'. Here is the
    code I wrote -

    cookie_obj = cookielib.CookieJar()

    # First page
    socket = urllib2.Request("http://www.zap2it.com/index")
    cookie_obj.add_cookie_header(socket)
    data = urllib2.urlopen(socket).read()
    print data

    # Second page
    socket =
    urllib2.Request("http://tvlistings2.zap2it.com/index.asp?partner_id=national&mash=mash1&zipcode=01810&submit1=Continue")
    cookie_obj.add_cookie_header(socket)
    data = urllib2.urlopen(socket).read()
    print data

    # Third Page
    socket =
    urllib2.Request("http://tvlistings2.zap2it.com/system.asp?partner_id=national&zipcode=01810")
    cookie_obj.add_cookie_header(socket)
    data = urllib2.urlopen(socket).read()
    print data


    Is there anything wrong in the above done code. I printed out the all
    the HTML headers in each stage to see if the cookie is getting added or
    not. I found out that the cookies are not getting added. But when I do
    "socket.info().headers" I get a list of all the headers wherein I can
    see 'Set-Cookie' tag and the cookie value.

    Anyone got any suggestions ?
    sri2097, Feb 22, 2006
    #1
    1. Advertising

  2. sri2097

    Paul Rubin Guest

    "sri2097" <> writes:
    > Hi, I need to get to a particular page in a website. The site uses
    > cookeis and naturally I had to use cookielib since urllib2 does not
    > support cookies.


    It's poorly documented but urllib2 does support cookies now.

    http://docs.python.org/lib/http-cookie-processor.html
    Paul Rubin, Feb 22, 2006
    #2
    1. Advertising

  3. sri2097

    Rene Pijlman Guest

    sri2097:
    >urllib2 does not support cookies.


    It does in 2.4. This code enables cookie support in all consequent calls
    through urrlib2:

    import cookielib, urllib2
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
    urllib2.install_opener(opener)

    --
    René Pijlman
    Rene Pijlman, Feb 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C. Titus Brown

    RFC 2965 cookies, cookielib, and mailman.

    C. Titus Brown, Dec 27, 2004, in forum: Python
    Replies:
    0
    Views:
    491
    C. Titus Brown
    Dec 27, 2004
  2. Alex Hunsley

    cookielib and urllib2: thread-safe?

    Alex Hunsley, Jan 26, 2005, in forum: Python
    Replies:
    1
    Views:
    467
    John J. Lee
    Jan 29, 2005
  3. John J Lee
    Replies:
    0
    Views:
    343
    John J Lee
    Jan 30, 2005
  4. Titus Brown
    Replies:
    0
    Views:
    468
    Titus Brown
    Feb 13, 2005
  5. Replies:
    0
    Views:
    562
Loading...

Share This Page