2016-01-19 16 views
0

正則表達式關鍵字我想提取時到期後引擎收錄與蟒蛇正則表達式,我的實際代碼日期:檢查和提取物與pastebin.com

#time_expired = re.compile('''title="When this paste gets automatically deleted"> (.*) 
#   </div>''') 

time_expired = re.compile('''title="When this paste gets automatically deleted">(.*)</div>''', re.DOTALL) 

def pastebin_check(pastebin_link): 
     br = Browser() 
     cj = cookielib.LWPCookieJar() 
     br.set_cookiejar(cj) 
     br.set_handle_equiv(True) 
     br.set_handle_redirect(True) 
     br.set_handle_referer(True) 
     br.set_handle_robots(False) 
     br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) 
     br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101')] 
     response = br.open(pastebin_link) 
     html = response.read() 

     print time_expired.search(html).group(1) 

隨着第(註釋掉)正則表達式的工作,與第二返回大量輸出只需'從不',我該如何修復我的正則表達式?

+0

添加您要申請正則表達式的示例文本! – SIslam

+0

此外,請顯示您希望提取的內容。 –

回答

0

您需要使用非正則表達式心切(.*?)

time_expired = re.compile('''title="When this paste gets automatically deleted">(.*?)</div>''', re.DOTALL) 
#notice the .*? 

當使用.*?re引擎會嘗試儘可能少趕上,你不會得到大的輸入。 (最後的</div>