【问题标题】:regex capture html hidden input正则表达式捕获html隐藏输入
【发布时间】:2013-10-06 20:48:58
【问题描述】:

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

【问题讨论】:

    标签: python html regex


    【解决方案1】:

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

    【讨论】:

    • AND 很好地从将 RE 模式包含在 "s 中转换为包含在 s 中,因此不再需要 \"
    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2011-04-13
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多