最近想用爬蟲爬一下知乎的數據,須要獲取網頁裏的防止 xsrf 的標記。session
「(?<=95|98|NT|2000)Windows」能匹配「2000Windows」中的「Windows」,但不能匹配「3.1Windows」中的「Windows」。url
即略去括號中的部分,只提取括號後的部分。3d
「Windows(?=95|98|NT|2000)」能匹配「Windows2000」中的「Windows」,但不能匹配「Windows3.1」中的「Windows」。code
即略去括號中的部分,只提取括號前的部分。token
s = requests.session() def get_xsrf(url=None): r = s.get(url, headers=headers_base) xsrf = re.search('(?<=name="_xsrf" value=")[^"]*(?="/>)', r.text) if xsrf == None: return ''
text 格式爲 '<input type="hidden" name="_xsrf" value="048ccfe3d47298fdcf31f9a6a44971a4"/>'字符串
使用 [^"]* 來提取 token 字符串,同時略去不須要的部分。get