How to get cookies from urllib.request?
12,414
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.
Author by
Admin
Updated on June 22, 2022Comments
-
Admin almost 2 years
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. -
Darren White almost 5 yearsI voted this down as it didn't answer the question - its good advice but not the answer.
-
user1602 over 4 yearsI agree, there is no answer here.