py中用re取一段字符串。
Set-Cookie: BAIDU_WAP_CKTIME=1345479535; Domain=wappass.baidu.com,; Path=/ Set-Cookie: BAIDU_WISE_UID=wpass_1345479535487_821; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZdnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBaAXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: PTOKEN=e273ab309524d33973ee9f7887ec4283; Domain=wappass.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: STOKEN=0c635c787b2c26ff9501bb956147ca62; Domain=wappass.baidu.com; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: WAPTIME=1345479535; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/
想要获取BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZdnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBaAXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV
琢磨了很久,都无从下手,望各位老鸟指点一二,感激不尽。
[解决办法]
非正则处理
#!python
# encoding: utf-8
content = '''Set-Cookie: BAIDU_WAP_CKTIME=1345479535; Domain=wappass.baidu.com,; Path=/ Set-Cookie: BAIDU_WISE_UID=wpass_1345479535487_821; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZdnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBaAXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: PTOKEN=e273ab309524d33973ee9f7887ec4283; Domain=wappass.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: STOKEN=0c635c787b2c26ff9501bb956147ca62; Domain=wappass.baidu.com; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: WAPTIME=1345479535; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/'''
for cookie in filter(lambda x: x.startswith('BDUSS='), content.split('Set-Cookie: ')):
item = filter(lambda x: x.startswith('BDUSS='), cookie.split('; '))[0]
print item
#!python
# encoding: utf-8
import re
patt = re.compile(r'Set-Cookie:\s(.+?)\/')
content = '''Set-Cookie: BAIDU_WAP_CKTIME=1345479535; Domain=wappass.baidu.com,; Path=/ Set-Cookie: BAIDU_WISE_UID=wpass_1345479535487_821; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZdnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBaAXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: PTOKEN=e273ab309524d33973ee9f7887ec4283; Domain=wappass.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: STOKEN=0c635c787b2c26ff9501bb956147ca62; Domain=wappass.baidu.com; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/ Set-Cookie: WAPTIME=1345479535; Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/'''
cookies = []
for items in patt.findall(content):
cookies.append(dict([item.split('=') for item in items.split('; ')]))
print [c['BDUSS'] for c in cookies if 'BDUSS' in c][0]
#========================================================
# author:ago
# 2012/08/21 9:09:10
#========================================================
import re
data = '''Set-Cookie: BAIDU_WAP_CKTIME=1345479535; \
Domain=wappass.baidu.com,; \
Path=/ Set-Cookie: BAIDU_WISE_UID=wpass_1345479535487_821; \
Domain=.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; \
Path=/ Set-Cookie: BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZ\
dnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~\
MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBa\
AXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV; Domain=.baidu.com,; \
expires=Thu, 20-Aug-2015 16:18:55 GMT; \
Path=/ Set-Cookie: PTOKEN=e273ab309524d33973ee9f7887ec4283; \
Domain=wappass.baidu.com,; expires=Thu, 20-Aug-2015 16:18:55 GMT; \
Path=/ Set-Cookie: STOKEN=0c635c787b2c26ff9501bb956147ca62; \
Domain=wappass.baidu.com; expires=Thu, 20-Aug-2015 16:18:55 GMT; \
Path=/ Set-Cookie: WAPTIME=1345479535; Domain=.baidu.com,; \
expires=Thu, 20-Aug-2015 16:18:55 GMT; Path=/'''
#print data
ret = re.findall( 'BDUSS=.+?;' , data )[0][:-1]
print ret
BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZdnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBaAXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV