2017-02-14 112 views
1

我试图纠正这个代码,并不断得到蟒蛇正则表达式,后面

sre_constants.error: look-behind requires fixed-width pattern 

请帮我摆脱这种错误的正面看...我所试图做的是让数这是变量w2,紧接在变量w的单词后面。

import requests 
import re 
import bs4 


def verse(book, chapter): 
     html = requests.get("http://www.holybible.or.kr/B_NIV/cgi/bibleftxt.php?VR=NIV&VL={}&CN={}&CV=99" 
          .format(book, chapter)).text 
     bs = bs4.BeautifulSoup(html, 'html5lib') 
     ol = bs.findAll('ol') 
     section_cnt = int(ol[-1].attrs['start']) + len(ol[-1].findAll('li')) - 1 
     w = re.search(r'(?<=height=12>\s<b>)(\d+\s)?[a-zA-Z]+\s[0-9]+', html).group() 
     w2 = re.search(r'(?<=height=12>\s<b>(\d+\s)?[a-zA-Z])+\s[0-9]+', html).group() 

     print(w, 'has', w2, 'chapters', section_cnt, 'verses') 

if __name__ == '__main__': 
    verse(1, 27) 
+0

从[re](https://docs.python.org/2/library/re.html)的文档中,'()'是一个特殊字符。如果你匹配的是(),你将需要用'\'来转义它。 –

回答

2

你不需要在这里看起来后面。

使用

(?:height=12>\s<b>(?:\d+\s)?[a-zA-Z]+)(\s[0-9]+) 

观看演示。

https://regex101.com/r/k1cYXS/1

获取group 1来代替。

w2 = re.search(r'(?:height=12>\s<b>(?:\d+\s)?[a-zA-Z]+)(\s[0-9]+)', html).group(1)