正则表达式捕获html隐藏输入

2024-06-16 08:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用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不工作:(,请有人帮帮我。在


Tags: 代码import功能rehtmlurllibcurlresults
1条回答
网友
1楼 · 发布于 2024-06-16 08:57:38

re.match从字符串的开头开始匹配,您可能希望re.search匹配字符串中的任何位置。在

Python docs

你的代码版本对我有用:

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')

相关问题 更多 >