1

我试图用 python pycurl 捕获 joomla 令牌,我写了这个函数代码:

import urllib, urllib2, sys, re
import cStringIO
import pycurl

def CaptureToken(cURL):
    buf = cStringIO.StringIO()
    c = pycurl.Curl()
    c.setopt(c.URL, cURL)
    c.setopt(c.WRITEFUNCTION, buf.write)
    c.setopt(c.CONNECTTIMEOUT, 30)
    c.setopt(c.TIMEOUT, 30)
    c.perform()
    html = buf.getvalue()
    buf.close()
    results = re.match(r"(type=\"hidden\" name=\"([0-9a-f]{32})\")", html).group(1)
    print results

CaptureToken('http://www.proregionisbono.org.pl/administrator/index.php')

在记事本++中这个正则表达式工作,在python中不工作:(,请有人帮助我。

4

1 回答 1

3

re.match从字符串的开头匹配,您可能想要re.search匹配字符串中的任何位置。

Python 文档

这个版本的代码对我有用:

import urllib, urllib2, sys, re
import cStringIO
import pycurl

def CaptureToken(cURL):
    buf = cStringIO.StringIO()
    c = pycurl.Curl()
    c.setopt(c.URL, cURL)
    c.setopt(c.WRITEFUNCTION, buf.write)
    c.setopt(c.CONNECTTIMEOUT, 30) 
    c.setopt(c.TIMEOUT, 30) 
    c.perform()
    html = buf.getvalue()
    buf.close()
    results = re.search(r'(type="hidden" name="([0-9a-f]{32})")', html).group(2)
    print results

CaptureToken('http://www.proregionisbono.org.pl/administrator/index.php')
于 2013-09-30T21:54:25.263 回答