Código:
devuelve el Error:from urllib.parse import urlencode from urllib.request import urlopen, Request, build_opener, HTTPCookieProcessor from http.cookiejar import CookieJar, Cookie datosLogin = {'var1':'valor1','var2':'valor2','var3':'valor3','var4':'valor4','var5':'valor5'} listaCookiesCreados = [] data = urlencode(datosLogin) data = data.encode('utf-8') request = Request("URLPrimeraPagina") request.add_header("User-Agent", 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1') request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8") f = urlopen(request, data) losCookieTraidos = [f.getheaders()[3],f.getheaders()[4],f.getheaders()[5],f.getheaders()[6],f.getheaders()[7]] for cookieIterado in losCookieTraidos: listaCookieIterado = cookieIterado[1].split(';') ''' if (len(listaCookieIterado) == 4) : cookie_expire =listaCookieIterado[1].split('=') cookie_expire = cookie_expire[1]''' cookie_session_id = listaCookieIterado[0].split('=') cookieCreada = Cookie( version=0, name=cookie_session_id[0], value=cookie_session_id[1], port=None, domain='http://alguno.com/', port_specified=False, domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, ) listaCookiesCreados.append(cookieCreada) print(listaCookiesCreados[4],listaCookiesCreados[1],listaCookiesCreados[2] ) cj = CookieJar() cj.set_policy(listaCookiesCreados[4]) cj.set_policy(listaCookiesCreados[1]) cj.set_policy(listaCookiesCreados[2]) opener = build_opener(HTTPCookieProcessor(cj)) opener.addheaders = [("User-Agent", 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1')] r = opener.open("URLSegundaPagina") print(r.info())
Código:
File "~/nada/untitled.py", line 57, in <module> r = opener.open("http://URLSegundaPagina/") File "/usr/lib/python3.2/urllib/request.py", line 368, in open req = meth(req) File "/usr/lib/python3.2/urllib/request.py", line 1188, in http_request self.cookiejar.add_cookie_header(request) File "/usr/lib/python3.2/http/cookiejar.py", line 1337, in add_cookie_header if (self._policy.rfc2965 and not self._policy.hide_cookie2 and AttributeError: 'Cookie' object has no attribute 'rfc2965'