社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何修复python中的“re.error:unterminated character set at position”?

DecstarG • 7 年前 • 4990 次点击  

我正在编写一个脚本,从网站上获取歌词” www.lyrics.com “。我有这个:

import os, string, re, requests

print("Enter lyrics.com site:")
url = input()

lyrics_raw_html = requests.get(url + '.html')
lyrics_raw = re.findall(r'<pre id=\"lyric-body-text\" class=\"lyric-body wselect-cnt\" dir=\"ltr\" data-lang=\"en\">([^]+)<\/pre>', lyrics_raw_html.text)
lyrics = re.sub(r'(<.+>)', '', lyrics_raw[0])

print(lyrics)

当我输入一页( this 页面例如)我得到这个错误:

File "C:\Users\MYNAMEHERE\AppData\Local\Programs\Python\Python37-32\lib\sre_parse.py", line 532, in _parse
source.tell() - here)
re.error: unterminated character set at position 91

它似乎是我的正则表达式,但经过一些修补,我不知道是什么问题。任何帮助都很好!

提前感谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47580
文章 [ 1 ]  |  最新文章 7 年前
Wiktor Stribiżew
Reply   •   1 楼
Wiktor Stribiżew    7 年前

在符合ECMAscript的正则表达式中(正则表达式可能是为该正则表达式引擎编写的), [^] 用于匹配任何字符,它是一个有效的字符类,它匹配“除零以外的任何内容”,因此匹配所有内容。

你应该使用 [\s\S]*? 在这里而不是 [^]+ 尽可能少地匹配任何0+字符。