简体   繁体   中英

How to get cookies from urllib.request?

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.

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.

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/

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM