Python中返回列表的正则表达式?
因此,我想用Python从大量HTML代码中创建一个列表,但我试图根据HTML标记将其拆分。我不太精通正则表达式,所以我不知道该怎么做。例如,假设我有一段HTML代码:Python中返回列表的正则表达式?,python,regex,Python,Regex,因此,我想用Python从大量HTML代码中创建一个列表,但我试图根据HTML标记将其拆分。我不太精通正则表达式,所以我不知道该怎么做。例如,假设我有一段HTML代码: <option value="674"> Example text here </option><option value="673"> Example text here</option><option value="672"> Example text here &
<option value="674"> Example text here </option><option value="673"> Example text here</option><option value="672"> Example text here </option>
无论如何,我可以这样做?我同意@roippi的评论,请使用HTML解析器。但是,如果您真的想使用正则表达式,那么以下是您想要的:
import re
s = '<option value="674"> Example text here </option><option value="673"> Example text here</option><option value="672"> Example text here </option>'
>>> print re.findall(r'>\s*([^<]+?)\s*<', s)
['Example text here', 'Example text here', 'Example text here']
重新导入
s='Example text here Example text here Example text here'
>>>print re.findall(r'>\s*)([^您可以简单地用于此目的
import bs4
html = '''
<option value="674"> Example text here </option>
<option value="673"> Example text here</option>
<option value="672"> Example text here </option>
'''
soup = bs4.BeautifulSoup(html)
mylst = [str(x.text).strip() for x in soup.find_all('option')]
现在停止使用HTML解析器。请。谢谢,我不知道这个库。
import bs4
html = '''
<option value="674"> Example text here </option>
<option value="673"> Example text here</option>
<option value="672"> Example text here </option>
'''
soup = bs4.BeautifulSoup(html)
mylst = [str(x.text).strip() for x in soup.find_all('option')]
['Example text here', 'Example text here', 'Example text here']