Need to get information from the header. The title took so www.info ()

Server: nginx / 0.8.54
Date: Tue, 17 May 2011 16:06:35 GMT
Content-Type: text / html; charset = utf-8
Transfer-Encoding: chunked
Connection: close
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Set-Cookie: sid = 6957034190070305; domain =. spaces.ru ; path = /

I need to get sid = 6957034190070305 and assign it to a variable. Punched to make a list ( www.info ()) - but only headers are in the variable, that is, "Server", "Date", "Set-Cookie", without values.

    2 answers 2

    import urllib www = urllib.urlopen('http://www.lenta.ru') www.info ().items() [('date', 'Tue, 17 May 2011 17:13:04 GMT'), ('content-type', 'text/html; charset=windows-1251'), ('connection', 'close'), ('vary', 'Accept-Encoding'), ('server', 'Apache')] www.info ()['date'] >>>'Tue, 17 May 2011 17:13:04 GMT' www.headers.items() [('date', 'Tue, 17 May 2011 17:13:04 GMT'), ('content-type', 'text/html; charset=windows-1251'), ('connection', 'close'), ('vary', 'Accept-Encoding'), ('server', 'Apache')] www.headers['date'] >>>'Tue, 17 May 2011 17:13:04 GMT' 

      I can prompt for urllib2 (Python 2.7)

       import urllib2 www = urllib2.urlopen('http://example.ru') cookie_header = www.headers['Set-Cookie'] 

      The output string, from which you can cut the desired parameter.

      • +1 for being 40 seconds ahead :) - yapycoder
      • mutually :) - Andrey Basalyga