2017-02-14 3 views
1

の背後にある正見て、私はこのコードを修正し、Pythonの正規表現、

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

を得続けるためにしようとしていますが、私が何をしようとしています何...このエラーを取り除く上で私を助けてください番号を取得することですこれは変数wである単語の直後にある変数w2です。

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)のドキュメントから、 '()'は特殊文字です。あなたが一致するものがcontains()なら '\'でエスケープする必要があります。 –

答えて

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)