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-8content = '''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 repatt = 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 redata = '''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 dataret = re.findall( 'BDUSS=.+?;' , data )[0][:-1]print ret
[解决办法]
输出:
BDUSS=jg5NTZueDdndlpMWEhEVGx0MlQ4dWY3NVNZdnhGeGlTZDZSdDJrUlZNQnZzUjlSFUFBQUFBJCQAAAAAAAAAAAokOCDUJ~MA0KHQodPj1q7OxwAAAAAAAAAAAAAAAAAAAAAAAAAAAACAYIArMAAAAOBaAXcAAAAAmWdCAAAAAAAxMC42NS4yM29jMlBvYzJQV
[解决办法]
来 这【脚本语言(Perl ,Python)】版块有点晚了,在这版块里,牛人很多啊而且回复的都很细心负责。
agoago_2009 牛人,每次回复问题,都非常专业和负责,嘻嘻。