How to get cookies from urllib.request?
Asked Answered
E

3

10

How to get cookie from an urllib.request?

import urllib.request
import urllib.parse

data = urllib.parse.urlencode({
    'user': 'user',
    'pass': 'pass'
})
data = data.encode('utf-8')

request = urllib.request.urlopen('http://example.com', data)
print(request.info())

request.info() returns cookies but not in very usable way.

Electrophysiology answered 8/8, 2014 at 18:41 Comment(2)
What is a "usable" way?Importation
you named request but that's a response object. The question is vague and could be solved in many different ways, but we don't know what are you trying to achieve (thus the "what is a usuable way?")Presence
T
6

response.info() is a dict type object. so you can parse any info you need. Here is a demo written in python3:

from urllib import request
from urllib.error import HTTPError

# declare url, header_params 

req = request.Request(url, data=None, headers=header_params, method='GET')
try:
    response = request.urlopen(req)

    cookie = response.info().get_all('Set-Cookie')
    content_type = response.info()['Content-Type']
except HTTPError as err:
    print("err status: {0}".format(err))
    return

You can now, parse cookie variable as your application requirement.

Trothplight answered 6/2, 2020 at 15:3 Comment(0)
A
0

Just used the following code to get cookie from Python Challenge #17, hope it helps (Python 3.8 being used):

import http.cookiejar
import urllib

cookiejar = http.cookiejar.CookieJar()
cookieproc = urllib.request.HTTPCookieProcessor(cookiejar)
opener = urllib.request.build_opener(cookieproc)
response = opener.open(url)
for cookie in cookiejar:
    print(cookie.name, cookie.value)
Areca answered 28/10, 2021 at 1:28 Comment(0)
A
-1

I think using the requests package is a much better choice these days. Try this sample code that shows google setting cookies when you visit:

import requests

url = "http://www.google.com"
r = requests.get(url,timeout=5)
if r.status_code == 200:
    for cookie in r.cookies:
        print(cookie)            # Use "print cookie" if you use Python 2.

Gives:

Cookie NID=67=n0l3ME1Jl3-wwlH7oE5pvxJ_CfU12hT5Kh65wh21bvE3hrKFAo1sJVj_UcuLCr76Ubi3yxENROaYNEitdgW4IttL43YZGlf8xAPl1IbzoLG31KP5U2tiP2y4DzVOJ2fA for .google.se/

Cookie PREF=ID=ce66d1288fc0d977:FF=0:TM=1407525509:LM=1407525509:S=LxQv7q8fju-iHJPZ for .google.se/
Aekerly answered 8/8, 2014 at 19:22 Comment(2)
I voted this down as it didn't answer the question - its good advice but not the answer.Pyroclastic
I agree, there is no answer here.Elusion

© 2022 - 2024 — McMap. All rights reserved.