在 urllib2.urlopen 中使用 mimetools.Message
我正在使用 urllib2.urlopen() :
req = urllib2.Request('http://www.google.com')
resp = urllib2.urlopen(req)
print resp.info()
print resp.info()['set-cookie']
Date: Sat, 14 May 2011 01:24:12 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=5ec78624283cc050:FF=0:TM=1305336252:LM=1305336252:S=eRXgUUuzhQbRmZxk; expires=Mon, 13-May-2013 01:24:12 GMT; path=/; domain=.google.com
Set-Cookie: NID=46=GxyZVeWbT9dn0sLa9waPGSusm1hFqGf46SPqewahg0bzbYIQX0oHff0bzJ33E2yO89npEsYkqSoX0HLSqHbCxj5tCK2E931PfEJbqDMB6lTDk4ngVAiiyObWmbHgRUC9; expires=Sun, 13-Nov-2011 01:24:12 GMT; path=/; domain=.google.com; HttpOnly
Server: gws
X-XSS-Protection: 1; mod
PREF=ID=5ec78624283cc050:FF=0:TM=1305336252:LM=1305336252:S=eRXgUUuzhQbRmZxk; expires=Mon, 13-May-2013 01:24:12 GMT; path=/; domain=.google.com, NID=46=GxyZVeWbT9dn0sLa9waPGSusm1hFqGf46SPqewahg0bzbYIQX0oHff0bzJ33E2yO89npEsYkqSoX0HLSqHbCxj5tCK2E931PfEJbqDMB6lTDk4ngVAiiyObWmbHgRUC9; expires=Sun, 13-Nov-2011 01:24:12 GMT; path=/; domain=.google.com; HttpOnly
正如您在响应中收到的标头中看到的,有两个“set-cookie”语句,但是在 resp.info( ) 对象我收到它已将两个 cookie 语句分组在一起并用“,”(逗号)分隔它们,
通过此分隔符分隔 cookie 很麻烦,因为我尝试分隔的 cookie 信息中有逗号使用此逗号分隔符
是否有一种简单的方法可以使用此 mimetools.message 对象单独调用每个 cookie 字符串? (resp.info()
)
else->我只需要手动解析标头,而不需要这个不太有用的 mimetools.message/dictionary 对象
I'm using urllib2.urlopen()
:
req = urllib2.Request('http://www.google.com')
resp = urllib2.urlopen(req)
print resp.info()
print resp.info()['set-cookie']
Date: Sat, 14 May 2011 01:24:12 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=5ec78624283cc050:FF=0:TM=1305336252:LM=1305336252:S=eRXgUUuzhQbRmZxk; expires=Mon, 13-May-2013 01:24:12 GMT; path=/; domain=.google.com
Set-Cookie: NID=46=GxyZVeWbT9dn0sLa9waPGSusm1hFqGf46SPqewahg0bzbYIQX0oHff0bzJ33E2yO89npEsYkqSoX0HLSqHbCxj5tCK2E931PfEJbqDMB6lTDk4ngVAiiyObWmbHgRUC9; expires=Sun, 13-Nov-2011 01:24:12 GMT; path=/; domain=.google.com; HttpOnly
Server: gws
X-XSS-Protection: 1; mod
PREF=ID=5ec78624283cc050:FF=0:TM=1305336252:LM=1305336252:S=eRXgUUuzhQbRmZxk; expires=Mon, 13-May-2013 01:24:12 GMT; path=/; domain=.google.com, NID=46=GxyZVeWbT9dn0sLa9waPGSusm1hFqGf46SPqewahg0bzbYIQX0oHff0bzJ33E2yO89npEsYkqSoX0HLSqHbCxj5tCK2E931PfEJbqDMB6lTDk4ngVAiiyObWmbHgRUC9; expires=Sun, 13-Nov-2011 01:24:12 GMT; path=/; domain=.google.com; HttpOnly
As you can see in the headers received in the response, there are TWO statements of 'set-cookie', HOWEVER in the resp.info()
object I receive it has grouped both cookie statements together and separates them by a ',' (comma)
This is troublesome to separate the cookies by this delimiter since there are commas inside the cookie information i'm try to separate with this comma delimiter
Is there an easy way to call upon each cookie string individually with this mimetools.message object? (resp.info()
)
else-> I'll just have to parse the headers manually without this not so helpful mimetools.message/dictionary object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
getheaders()
来获取 cookie 的列表
:在本例中,您将获得两个
字符串
的列表
。然后您可以迭代该列表并获取您喜欢的任何 cookie。
str.startswith()
是你的朋友:新手如何找到Python文档
Try using
getheaders()
to get alist
of the cookies:In this case, you get a
list
of twostring
s.Then you can iterate over that
list
and grab whichever cookie you like.str.startswith()
is your friend:How a newbie can find the documentation in Python