302s and losing cookies with urllib2

后端 未结 4 1916
旧时难觅i
旧时难觅i 2021-01-21 06:13

I am using liburl2 with CookieJar / HTTPCookieProcessor in an attempt to simulate a login to a page to automate an upload.

I\'ve seen some questions and answers on this,

4条回答
  •  Happy的楠姐
    2021-01-21 07:05

    I've just got a variation of the below working for me, at least when trying to read Atom from http://www.fudzilla.com/home?format=feed&type=atom

    I can't verify that the below snippet will run as-is, but might give you a start:

    import cookielib
    cookie_jar = cookielib.LWPCookieJar()
    cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
    handlers = [cookie_handler] #+others, we have proxy + progress handlers
    opener = apply(urllib2.build_opener, tuple(handlers + [_FeedURLHandler()])) #see http://code.google.com/p/feedparser/source/browse/trunk/feedparser/feedparser.py#2848 for implementation of _FeedURLHandler
    opener.addheaders = [] #may not be needed but see the comments around the link referred to below
    try:
        return opener.open(request) #see http://code.google.com/p/feedparser/source/browse/trunk/feedparser/feedparser.py#2954 for implementation of request
    finally:
        opener.close()
    

提交回复
热议问题